Recall & Review
beginner
What function is used to open a file for reading in C?
The
fopen() function is used to open a file. To open a file for reading, use fopen(filename, "r").Click to reveal answer
beginner
How do you read a single character from a file in C?
Use the
fgetc() function. It reads one character from the file and returns it as an int.Click to reveal answer
intermediate
What does
fgets() do when reading from a file?fgets() reads a line or up to a specified number of characters from a file into a string, stopping at a newline or EOF.Click to reveal answer
beginner
Why should you always check the return value of
fopen()?Because
fopen() returns NULL if the file cannot be opened (e.g., file does not exist or no permission). Checking prevents errors later.Click to reveal answer
beginner
How do you properly close a file after reading in C?
Use the
fclose() function with the file pointer to close the file and free resources.Click to reveal answer
Which mode should you use with
fopen() to read a file?✗ Incorrect
Use "r" mode to open a file for reading only.
What does
fgetc() return when it reaches the end of a file?✗ Incorrect
fgetc() returns EOF (usually -1) when it reaches the end of the file.Which function reads a whole line from a file into a string?
✗ Incorrect
fgets() reads a line or up to a specified number of characters from a file.What should you do if
fopen() returns NULL?✗ Incorrect
If
fopen() returns NULL, the file could not be opened, so you should handle the error.Which function closes a file in C?
✗ Incorrect
fclose() closes the file and releases resources.Explain the steps to read text from a file in C.
Think about opening, reading, checking errors, and closing.
You got /4 concepts.
What are common errors to watch for when reading files in C?
Consider what can go wrong at each step.
You got /4 concepts.