What function is used to open a file for writing in C?
The fopen() function is used to open a file. To open a file for writing, you use fopen(filename, "w").
Click to reveal answer
beginner
How do you write a string to a file in C?
You use the fprintf() or fputs() functions. For example, fprintf(file, "%s", "Hello") writes the string "Hello" to the file.
Click to reveal answer
beginner
What must you always do after finishing writing to a file?
You must close the file using fclose(file) to save changes and free resources.
Click to reveal answer
intermediate
What happens if you open a file with mode "w" and the file already exists?
The existing file is cleared (emptied) before writing starts. If the file does not exist, a new file is created.
Click to reveal answer
beginner
How can you check if a file was opened successfully?
After calling fopen(), check if the returned pointer is NULL. If it is NULL, the file did not open successfully.
Click to reveal answer
Which mode opens a file for writing and clears its contents if it exists?
A"w"
B"r"
C"a"
D"r+"
✗ Incorrect
Mode "w" opens a file for writing and clears existing content or creates a new file.
What does fclose() do in file handling?
ADeletes the file
BOpens the file for reading
CReads data from the file
DCloses the file and saves changes
✗ Incorrect
fclose() closes the file and ensures all data is saved properly.
Which function writes formatted text to a file?
Afread()
Bfprintf()
Cfclose()
Dfopen()
✗ Incorrect
fprintf() writes formatted text to a file.
How do you check if fopen() failed to open a file?
ACheck if the file exists
BCheck if the file size is zero
CCheck if the returned pointer is NULL
DCheck if the file is empty
✗ Incorrect
If fopen() returns NULL, it means the file could not be opened.
Which function writes a string to a file without formatting?
Afputs()
Bfprintf()
Cfread()
Dfclose()
✗ Incorrect
fputs() writes a string directly to a file without formatting.
Explain the steps to write text to a file in C.
Think about opening, writing, and closing the file.
You got /4 concepts.
What happens if you open a file with "w" mode and the file already exists?
Consider how "w" mode treats existing files.
You got /3 concepts.
Practice
(1/5)
1. What does the mode "w" mean when used with fopen in C?
easy
A. Open a file for writing, creating it if it doesn't exist, or overwriting if it does.
B. Open a file for reading only.
C. Open a file for appending data at the end.
D. Open a file in binary mode for reading and writing.
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 A
Quick Check:
fopen mode "w" = write with overwrite [OK]
Hint: Remember: "w" means write and overwrite file [OK]
Common Mistakes:
Confusing "w" with "a" (append mode)
Thinking "w" opens file for reading
Assuming "w" preserves existing content
2. Which of the following is the correct way to open a file named "data.txt" for writing in C?
easy
A. FILE *fp = fopen("data.txt", "w");
B. FILE *fp = fopen("data.txt", "r");
C. FILE *fp = fopen("data.txt", "a");
D. FILE *fp = open("data.txt", "w");
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 A
Quick Check:
Use fopen with "w" to write [OK]
Hint: Use fopen with "w" to write files in C [OK]
Common Mistakes:
Using open() instead of fopen()
Using wrong mode like "r" or "a"
Forgetting to use double quotes around filename
3. What will be the output in the file "output.txt" after running this code?
B. File name should be a variable, not a string literal
C. fprintf syntax is wrong, needs format specifier
D. Missing fclose(fp) to close the file
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 D
Quick Check:
Always close files with fclose [OK]
Hint: Always fclose after writing to file [OK]
Common Mistakes:
Forgetting fclose causes data loss
Confusing fopen modes
Thinking fprintf needs format specifier always
5. You want to write multiple lines to a file "log.txt" in C. Which code snippet correctly writes these lines and ensures the file is properly handled?
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 B
Quick Check:
"w" + multiple fprintf + fclose [OK]
Hint: Use "w" mode and always fclose [OK]
Common Mistakes:
Using "r" mode when writing
Forgetting fclose after writing
Not understanding difference between "w" and "a"
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.