0
0
Cprogramming~5 mins

Reading from 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 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?
A"r"
B"w"
C"a"
D"rw"
What does fgetc() return when it reaches the end of a file?
A-1
BEOF
CNULL
D0
Which function reads a whole line from a file into a string?
Afgetc()
Bfprintf()
Cfread()
Dfgets()
What should you do if fopen() returns NULL?
AHandle the error, e.g., show a message and stop reading
BIgnore it and continue
CTry to write to the file
DClose the file pointer
Which function closes a file in C?
Afileclose()
Bclose()
Cfclose()
Dendfile()
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.