File modes in C - Time & Space Complexity
When working with files in C, the mode you choose affects how the program reads or writes data.
We want to understand how the time to open and process a file changes as the file size grows.
Analyze the time complexity of the following code snippet.
FILE *file = fopen("data.txt", "r");
char buffer[256];
while (fgets(buffer, sizeof(buffer), file)) {
// process the line
}
fclose(file);
This code opens a file in read mode and reads it line by line until the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each line from the file using fgets.
- How many times: Once for every line in the file until the end.
As the file gets bigger, the program reads more lines, so it takes longer.
| Input Size (lines) | Approx. Operations (reads) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of operations grows directly with the number of lines in the file.
Time Complexity: O(n)
This means the time to read the file grows linearly with the number of lines.
[X] Wrong: "Opening a file is slow and depends on file size."
[OK] Correct: Opening a file is usually a quick operation; the time depends mostly on how much data you read or write, not just opening.
Understanding how file reading scales helps you write efficient programs and answer questions about handling large data files.
"What if we changed the mode from reading line by line to reading the entire file at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand file modes in C
"r" mode opens a file for reading only, without modifying it.Step 2: Compare other modes
"w" mode opens for writing and clears the file; "a" mode appends; "rw" is invalid in C.Final Answer:
"r" mode -> Option BQuick Check:
Read-only mode = "r" [OK]
- Confusing "w" with read mode
- Using "rw" which is invalid in C
- Thinking "a" allows reading
data.txt for writing in C?Solution
Step 1: Identify correct function and mode
In C,fopenopens files; "w" mode opens for writing.Step 2: Check other options
FILE *fp = fopen("data.txt", "r"); uses "r" (read), C uses "a" (append), D uses wrong functionopen.Final Answer:
FILE *fp = fopen("data.txt", "w"); -> Option AQuick Check:
Write mode uses fopen with "w" [OK]
- Using open() instead of fopen()
- Confusing "r" with write mode
- Using "a" when write mode is needed
FILE *fp = fopen("test.txt", "w");
fputs("Hello", fp);
fclose(fp);
fp = fopen("test.txt", "a");
fputs(" World", fp);
fclose(fp);
fp = fopen("test.txt", "r");
char buffer[20];
fgets(buffer, 20, fp);
printf("%s", buffer);
fclose(fp);Solution
Step 1: Write "Hello" to file with "w" mode
Opening with "w" clears file and writes "Hello".Step 2: Append " World" with "a" mode
Opening with "a" adds " World" at the end without erasing.Step 3: Read file content with "r" mode
Reading file returns "Hello World" in buffer.Final Answer:
Hello World -> Option CQuick Check:
Write + Append + Read = "Hello World" [OK]
- Assuming "a" overwrites file
- Forgetting "w" clears file
- Expecting only "Hello" output
FILE *fp;
fp = fopen("log.txt", "wa");
if (fp == NULL) {
printf("Failed to open file\n");
}Solution
Step 1: Check file mode string validity
"wa" is not a valid mode; modes must be "r", "w", "a" or with "b" for binary.Step 2: Understand consequences
Invalid mode causes fopen to fail and return NULL.Final Answer:
Incorrect file mode string "wa" -> Option AQuick Check:
File modes must be valid strings like "a" or "w" [OK]
- Combining modes incorrectly like "wa"
- Ignoring fopen failure check
- Assuming "wa" means write and append
Solution
Step 1: Understand log file requirements
Logs must keep old entries and add new ones without erasing.Step 2: Match file mode to requirement
"a" mode appends data at the end, preserving existing content.Step 3: Evaluate other modes
"w" clears file, "r" only reads, "r+" reads/writes but doesn't append automatically.Final Answer:
"a" mode, because it appends new logs preserving old content -> Option DQuick Check:
Append mode "a" preserves and adds data [OK]
- Using "w" which erases old logs
- Thinking "r" can write
- Confusing "r+" with append mode
Ready to prove it?
Pass the test and this pattern moves unknown
