Writing to files lets your program save information so you can use it later or share it with others.
Writing to files in C
FILE *file_pointer; file_pointer = fopen("filename.txt", "w"); if (file_pointer != NULL) { fprintf(file_pointer, "text to write\n"); fclose(file_pointer); }
fopen opens a file. The "w" means write mode, which creates or overwrites the file.
fprintf writes formatted text to the file, like printf but for files.
FILE *fp = fopen("data.txt", "w"); if (fp != NULL) { fprintf(fp, "Hello, file!\n"); fclose(fp); }
FILE *fp = fopen("log.txt", "a"); if (fp != NULL) { fprintf(fp, "New log entry\n"); fclose(fp); }
This program opens a file named example.txt for writing, writes two lines, closes the file, and prints a success message.
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "w"); if (file == NULL) { printf("Error opening file.\n"); return 1; } fprintf(file, "This is a line of text.\n"); fprintf(file, "Writing to files is easy!\n"); fclose(file); printf("Data written to example.txt successfully.\n"); return 0; }
Always check if fopen returns NULL to avoid errors when the file can't be opened.
Remember to close the file with fclose to save changes and free resources.
Using "w" mode deletes existing file content; use "a" to add without deleting.
Use fopen with "w" to open a file for writing (creates or overwrites).
Use fprintf to write text to the file.
Always close the file with fclose when done.
Practice
fopen in C?Solution
Step 1: Understand fopen modes
The mode "w" in fopen means open for writing. If the file exists, it will be overwritten. If it doesn't exist, it will be created.Step 2: Compare with other modes
Other modes like "r" are for reading, "a" for appending, and "wb" for binary write. "w" specifically means write with overwrite or create.Final Answer:
Open a file for writing, creating it if it doesn't exist, or overwriting if it does. -> Option AQuick Check:
fopen mode "w" = write with overwrite [OK]
- Confusing "w" with "a" (append mode)
- Thinking "w" opens file for reading
- Assuming "w" preserves existing content
Solution
Step 1: Identify correct function and mode
In C, to open a file for writing, use fopen with mode "w". The function is fopen, not open.Step 2: Check each option
FILE *fp = fopen("data.txt", "r"); uses mode "r" (read), not writing. FILE *fp = fopen("data.txt", "a"); uses "a" (append). FILE *fp = open("data.txt", "w"); uses open, which is not standard C file open function.Final Answer:
FILE *fp = fopen("data.txt", "w"); -> Option AQuick Check:
Use fopen with "w" to write [OK]
- Using open() instead of fopen()
- Using wrong mode like "r" or "a"
- Forgetting to use double quotes around filename
FILE *fp = fopen("output.txt", "w");
fprintf(fp, "Hello %s!", "World");
fclose(fp);Solution
Step 1: Understand fprintf usage
fprintf writes formatted text to the file. The format string "Hello %s!" expects a string argument, which is "World" here.Step 2: Determine file content
The file will contain the formatted string with %s replaced by "World", so the file content is "Hello World!".Final Answer:
Hello World! -> Option CQuick Check:
fprintf formats string correctly [OK]
- Thinking %s prints literally
- Forgetting to pass argument for %s
- Assuming fprintf prints to console
FILE *fp;
fp = fopen("test.txt", "w");
fprintf(fp, "Test");Solution
Step 1: Check file opening and writing
The file is opened correctly with fopen and mode "w". fprintf syntax is correct to write "Test".Step 2: Check for missing file closure
The code does not call fclose(fp), which is necessary to close the file and flush buffers properly.Final Answer:
Missing fclose(fp) to close the file -> Option DQuick Check:
Always close files with fclose [OK]
- Forgetting fclose causes data loss
- Confusing fopen modes
- Thinking fprintf needs format specifier always
1) FILE *fp = fopen("log.txt", "w");
fprintf(fp, "Line 1\n");
fprintf(fp, "Line 2\n");
fclose(fp);
2) FILE *fp = fopen("log.txt", "r");
fprintf(fp, "Line 1\n");
fclose(fp);
3) FILE *fp = fopen("log.txt", "w");
fprintf(fp, "Line 1\nLine 2\n");
// missing fclose
4) FILE *fp = fopen("log.txt", "a");
fprintf(fp, "Line 1\n");
fclose(fp);Solution
Step 1: Analyze each option's correctness
Option 1 opens file with "w" (write), writes two lines separately, and closes file properly. This is correct.Step 2: Check other options
Option 2 opens with "r" (read), so writing causes error. Option 3 writes lines but misses fclose, risking data loss. Option 4 opens with "a" (append), writes only one line, but the goal is multiple lines.Final Answer:
Option 1 only -> Option BQuick Check:
"w" + multiple fprintf + fclose [OK]
- Using "r" mode when writing
- Forgetting fclose after writing
- Not understanding difference between "w" and "a"
Ready to prove it?
Pass the test and this pattern moves unknown
