File pointers in C - Time & Space Complexity
When working with file pointers in C, it's important to understand how the time to read or write data grows as the file size changes.
We want to know how the program's running time changes when it processes larger files.
Analyze the time complexity of the following code snippet.
FILE *fp = fopen("data.txt", "r");
char buffer[100];
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
// process the line
}
fclose(fp);
This code reads a file line by line until it reaches the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each line from the file using fgets inside a while loop.
- How many times: Once for every line in the file until the end is reached.
As the number of lines in the file grows, the number of times the loop runs grows roughly the same.
| Input Size (n 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 in a straight line with the number of lines.
[X] Wrong: "Reading a file always takes the same time no matter its size."
[OK] Correct: The program reads each line one by one, so more lines mean more work and more time.
Understanding how file reading time grows helps you write efficient programs that handle large data smoothly.
"What if we read the file character by character instead of line by line? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a file pointer is
A file pointer is a special pointer that points to a file stream opened by the program.Step 2: Differentiate from other options
It is not the file contents or a function, but a reference to the file for reading or writing.Final Answer:
A reference to a file opened in the program -> Option BQuick Check:
File pointer = Reference to opened file [OK]
- Thinking file pointer stores file data
- Confusing file pointer with file reading function
- Assuming file pointer is a file format
Solution
Step 1: Recall fopen syntax
The fopen function returns a pointer to FILE, so the variable must be FILE *fp.Step 2: Check each option
FILE *fp = fopen("file.txt", "r"); correctly declares FILE *fp and calls fopen with filename and mode "r".Final Answer:
FILE *fp = fopen("file.txt", "r"); -> Option AQuick Check:
Correct fopen syntax = FILE *fp = fopen("file.txt", "r"); [OK]
- Declaring FILE variable instead of pointer
- Using open() instead of fopen()
- Incorrect order of fopen parameters
FILE *fp = fopen("test.txt", "w");
fprintf(fp, "Hello\n");
fclose(fp);
fp = fopen("test.txt", "r");
char buffer[10];
fgets(buffer, 10, fp);
printf("%s", buffer);
fclose(fp);Solution
Step 1: Write and close file
The code opens test.txt for writing, writes "Hello\n", then closes the file.Step 2: Read and print file content
It reopens test.txt for reading, reads a line into buffer, then prints it. fgets reads until newline or buffer limit.Final Answer:
Hello (printed as Hello with newline) -> Option DQuick Check:
File write/read prints "Hello" [OK]
- Expecting literal \n printed
- Confusing fprintf and printf output
- Forgetting to close file before reopening
FILE *fp;
fp = fopen("data.txt", "r");
char c = fgetc(fp);
while (c != EOF) {
putchar(c);
c = fgetc(fp);
}
fclose(fp);Solution
Step 1: Check fgetc return type
fgetc returns int to allow EOF (-1) detection, storing in char loses EOF info.Step 2: Understand EOF check failure
Comparing char c to EOF fails because c cannot hold EOF value properly, causing infinite loop or wrong output.Final Answer:
fgetc returns int, not char, causing wrong EOF check -> Option AQuick Check:
Use int for fgetc result to detect EOF [OK]
- Using char instead of int for fgetc result
- Ignoring EOF check causing infinite loops
- Assuming putchar can't print chars
Solution
Step 1: Understand file modes
"a" mode opens file for appending, preserving existing content and writing at the end.Step 2: Compare other modes
"w" overwrites file, "r" is read-only, "r+" allows read/write but does not append automatically.Final Answer:
FILE *fp = fopen("hello.txt", "a"); -> Option CQuick Check:
Append mode = "a" [OK]
- Using "w" which erases file content
- Using "r" which is read-only
- Confusing "r+" with append mode
Ready to prove it?
Pass the test and this pattern moves unknown
