Recall & Review
beginner
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?
✗ Incorrect
Mode "w" opens a file for writing and clears existing content or creates a new file.
What does
fclose() do in file handling?✗ Incorrect
fclose() closes the file and ensures all data is saved properly.Which function writes formatted text to a file?
✗ Incorrect
fprintf() writes formatted text to a file.How do you check if
fopen() failed to open a file?✗ Incorrect
If
fopen() returns NULL, it means the file could not be opened.Which function writes a string to a file without formatting?
✗ 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.