Which function is used in C to write formatted data to a file?
A
printf() B
fprintf() C
write() D
fwrite()
Analysis & Theory
`fprintf()` is used to write formatted output to a file.
Which mode in `fopen()` is used to write to a file (and erase existing content)?
A
"r" B
"w" C
"a" D
"rw"
Analysis & Theory
`"w"` mode opens a file for writing and clears its contents if it exists.
Which of the following correctly opens a file for writing?
A
fopen("data.txt", "r") B
fopen("data.txt", "w") C
openfile("data.txt") D
fwrite("data.txt")
Analysis & Theory
`fopen("filename", "w")` is used to open a file in write mode.
What will happen if the file cannot be opened using `fopen()`?
A
It creates a new file automatically B
It returns NULL C
It exits the program D
It throws a compile-time error
Analysis & Theory
`fopen()` returns `NULL` if the file cannot be opened.
Which function is used to close a file in C?
A
endfile() B
fileclose() C
fend() D
fclose()
Analysis & Theory
`fclose()` is used to close an opened file.
What does the following code do?
```c
FILE *fp = fopen("out.txt", "w");
fprintf(fp, "Hello");
fclose(fp);
```
A
Prints 'Hello' to screen B
Writes 'Hello' to out.txt C
Reads 'Hello' from out.txt D
Gives a syntax error
Analysis & Theory
The code writes 'Hello' to the file `out.txt`.
Which function is used to write a string (without formatting) to a file?
A
fprintf() B
fputs() C
fscanf() D
fwrite()
Analysis & Theory
`fputs()` is used to write strings to files without formatting.
What mode would you use to **append** data to an existing file?
A
"w" B
"r" C
"a" D
"wa"
Analysis & Theory
`"a"` mode appends data to the end of the file.
Which data type is used for declaring file pointers?
A
file B
FILE* C
fstream D
pointer
Analysis & Theory
In C, `FILE*` is used for declaring file pointers.
What should you always do after finishing writing to a file?
A
Save the file B
Flush the buffer C
Free the pointer D
Close the file using fclose()
Analysis & Theory
`fclose()` must be called to close the file and ensure data is saved.