What is the purpose of error handling when working with files in C?
Error handling helps detect and respond to problems like missing files, permission issues, or read/write errors to prevent program crashes and data loss.
Click to reveal answer
beginner
Which function is commonly used to open a file and how do you check if it failed?
The fopen() function opens a file. If it returns NULL, it means the file could not be opened, indicating an error.
Click to reveal answer
intermediate
How can you get more information about the error after a file operation fails?
You can use the global variable errno and functions like perror() or strerror() to get a human-readable error message.
Click to reveal answer
beginner
What should you always do after finishing file operations?
Always close the file using fclose() to free resources and avoid data corruption.
Click to reveal answer
beginner
Show a simple C code snippet that opens a file for reading and handles the error if the file does not exist.
#include
int main() {
FILE *file = fopen("data.txt", "r");
if (file == NULL) {
perror("Error opening file");
return 1;
}
// File operations here
fclose(file);
return 0;
}
Click to reveal answer
What does fopen() return if it fails to open a file?
ANULL
B0
C-1
DA file pointer
✗ Incorrect
fopen() returns NULL when it cannot open the file.
Which function prints a descriptive error message related to the last file operation?
Aprintf()
Bperror()
Cfclose()
Dfopen()
✗ Incorrect
perror() prints a message describing the last error.
Why should you always call fclose() after finishing file operations?
ATo open the file again
BTo check for errors
CTo delete the file
DTo free resources and save data properly
✗ Incorrect
fclose() frees resources and ensures data is saved correctly.
What global variable holds the error code after a failed file operation?
Aerrno
Berror_code
Cfile_error
Dstatus
✗ Incorrect
errno stores the error code after a failure.
If fopen() returns NULL, what is the best next step?
AIgnore and continue
BCall <code>fclose()</code>
CUse <code>perror()</code> to print the error and handle it
DTry to write to the file
✗ Incorrect
You should print the error with perror() and handle the failure properly.
Explain how to safely open a file in C and handle the case when the file cannot be opened.
Think about checking the return value of fopen and printing an error message.
You got /4 concepts.
Describe why closing a file with fclose is important after file operations.
Consider what happens if you don't close a file properly.
You got /4 concepts.
Practice
(1/5)
1. What does fopen return if it fails to open a file in C?
easy
A. 0
B. An empty string
C. -1
D. NULL
Solution
Step 1: Understand fopen behavior
The fopen function returns a pointer to a FILE object if successful.
Step 2: Identify failure return value
If fopen fails (e.g., file not found), it returns NULL to indicate error.
Final Answer:
NULL -> Option D
Quick Check:
fopen failure returns NULL [OK]
Hint: Remember: fopen returns NULL on failure, not zero or -1 [OK]
Common Mistakes:
Thinking fopen returns 0 on failure
Confusing NULL with empty string
Assuming fopen throws an error instead of returning NULL
2. Which of the following is the correct way to check if a file was opened successfully in C?
easy
A. if (file != NULL)
B. if (file == 0)
C. if (file == -1)
D. if (file == NULL)
Solution
Step 1: Recall fopen return value on failure
fopen returns NULL if it fails to open a file.
Step 2: Write correct check for success
To check if opened successfully, compare the file pointer using file != NULL.
Final Answer:
if (file != NULL) -> Option A
Quick Check:
Check file pointer against NULL for success [OK]
Hint: Check if file pointer != NULL to confirm open success [OK]
Common Mistakes:
Using 0 or -1 instead of NULL for pointer check
Checking if file == NULL to confirm success
Confusing integer and pointer comparisons
3. What will be the output of this C code snippet?
#include <stdio.h>
int main() {
FILE *fp = fopen("nonexistent.txt", "r");
if (fp == NULL) {
printf("File open failed\n");
} else {
printf("File opened\n");
fclose(fp);
}
return 0;
}
medium
A. File opened
B. File open failed
C. Compilation error
D. No output
Solution
Step 1: fopen tries to open a non-existing file
The file "nonexistent.txt" does not exist, so fopen returns NULL.
Step 2: Check the if condition and output
Since fp == NULL is true, the program prints "File open failed".
Final Answer:
File open failed -> Option B
Quick Check:
fopen fails, prints failure message [OK]
Hint: If fopen returns NULL, print error message [OK]
Common Mistakes:
Assuming file opens successfully
Ignoring the NULL check
Expecting compilation error due to fopen
4. Identify the error in this code snippet that opens and closes a file: