Why file handling is required in C - Performance Analysis
We want to understand how the time to run file handling operations changes as the size of the file grows.
How does reading or writing more data affect the time it takes?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
int main() {
FILE *file = fopen("data.txt", "r");
int ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
return 0;
}
This code reads a file character by character and prints it to the screen.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each character from the file one by one.
- How many times: Once for every character in the file until the end.
As the file size grows, the program reads more characters, so it takes more time.
| Input Size (characters) | Approx. Operations |
|---|---|
| 10 | About 10 reads |
| 100 | About 100 reads |
| 1000 | About 1000 reads |
Pattern observation: The time grows directly with the number of characters in the file.
Time Complexity: O(n)
This means the time to read the file grows in a straight line with the file size.
[X] Wrong: "Reading a file always takes the same time no matter how big it is."
[OK] Correct: The program reads each character one by one, so bigger files take more time.
Understanding how file size affects reading time helps you write efficient programs that handle data well.
"What if we read the file in larger chunks instead of one character at a time? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of file handling
File handling allows programs to store data permanently on disk, beyond the program's runtime.Step 2: Compare options with this purpose
Only saving data permanently matches the main reason for file handling; other options are unrelated.Final Answer:
To save data permanently outside the program -> Option CQuick Check:
File handling = permanent data storage [OK]
- Thinking file handling speeds up program
- Confusing memory use with file storage
- Believing file handling avoids variables
Solution
Step 1: Recall fopen syntax in C
fopen requires a FILE pointer and mode string; "w" mode opens file for writing.Step 2: Check each option
FILE *fp = fopen("data.txt", "w"); uses FILE *fp and fopen with "w" correctly; others have wrong function, mode, or variable type.Final Answer:
FILE *fp = fopen("data.txt", "w"); -> Option DQuick Check:
Open file for writing = fopen with "w" [OK]
- Using 'r' mode to write
- Using open() instead of fopen()
- Declaring FILE variable instead of pointer
FILE *fp = fopen("test.txt", "w");
fprintf(fp, "Hello World");
fclose(fp);
fp = fopen("test.txt", "r");
char str[20];
fgets(str, 20, fp);
printf("%s", str);
fclose(fp);Solution
Step 1: Write and close file
The code opens "test.txt" for writing, writes "Hello World", then closes the file.Step 2: Open file for reading and read content
It reopens the file for reading, reads the string into str, and prints it.Final Answer:
Hello World -> Option BQuick Check:
Write then read file prints saved text [OK]
- Forgetting to close file before reading
- Using wrong mode for fopen
- Not reading file content before printing
FILE *fp;
fp = fopen("data.txt", "r");
fprintf(fp, "Test");
fclose(fp);Solution
Step 1: Check file open mode
The file is opened in "r" mode, which is read-only.Step 2: Check usage of fprintf
fprintf writes to file, but writing is not allowed in read mode, causing error.Final Answer:
Using fprintf on a file opened in read mode -> Option AQuick Check:
Write requires "w" mode, not "r" [OK]
- Writing to read-only file
- Ignoring file open mode
- Not checking fopen return value
Solution
Step 1: Understand file modes
"w" overwrites file, "r" reads only, "a" appends data at end, "r+" reads and writes but does not append.Step 2: Choose mode for adding without deleting
Append mode "a" adds new data without deleting existing content.Final Answer:
"a" (append mode) -> Option AQuick Check:
Append mode = add data without deleting [OK]
- Using "w" which erases old data
- Confusing "r+" with append mode
- Trying to write in "r" mode
Ready to prove it?
Pass the test and this pattern moves unknown
