0
0
Cprogramming~5 mins

Writing to files in C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A"w"
B"r"
C"a"
D"r+"
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
Which function writes formatted text to a file?
Afread()
Bfprintf()
Cfclose()
Dfopen()
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
Which function writes a string to a file without formatting?
Afputs()
Bfprintf()
Cfread()
Dfclose()
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.