The fopen() function is used to open a file in C. It requires the file name and mode (like "r" for read, "w" for write).
Click to reveal answer
beginner
What does the mode "r" mean when opening a file with fopen()?
The mode "r" means open the file for reading only. The file must exist, or fopen() will return NULL.
Click to reveal answer
beginner
How do you properly close a file in C?
Use the fclose() function and pass the file pointer returned by fopen(). This frees resources and saves changes.
Click to reveal answer
intermediate
What happens if you forget to close a file after opening it?
If you forget to close a file, it can cause memory leaks or data not being saved properly. Always close files to keep your program safe.
Click to reveal answer
beginner
What does fopen() return if it fails to open a file?
fopen() returns NULL if it cannot open the file (for example, if the file does not exist in read mode). Always check for NULL before using the file pointer.
Click to reveal answer
Which function opens a file in C?
Afileopen()
Bfopen()
Copenfile()
Dopen()
✗ Incorrect
The correct function to open a file in C is fopen().
What mode should you use with fopen() to write to a file?
A"w"
B"rw"
C"a"
D"r"
✗ Incorrect
Use mode "w" to open a file for writing. It creates the file if it doesn't exist or truncates it if it does.
What does fclose() do?
AReads data from a file
BDeletes a file
CWrites data to a file
DCloses an open file
✗ Incorrect
fclose() closes an open file and releases resources.
If fopen() fails to open a file, what does it return?
A-1
B0
CNULL
Dfalse
✗ Incorrect
fopen() returns NULL if it cannot open the file.
Why should you always close files after opening them?
ATo free memory and save data
BTo speed up the program
CTo delete the file
DTo rename the file
✗ Incorrect
Closing files frees memory and ensures data is saved properly.
Explain how to open a file for reading and then close it in C.
Think about the steps: open, check, use, close.
You got /4 concepts.
What are the risks of not closing a file after opening it?
Consider what happens inside the computer when files stay open.
You got /3 concepts.
Practice
(1/5)
1. What does the fopen function do in C programming?
easy
A. It closes an open file stream.
B. It opens a file and returns a pointer to the file stream.
C. It reads data from a file.
D. It writes data to a file.
Solution
Step 1: Understand the purpose of fopen
The fopen function is used to open a file and returns a pointer to the file stream if successful.
Step 2: Differentiate fopen from other file functions
Functions like fclose close files, fread reads data, and fwrite writes data. Only fopen opens files.
Final Answer:
It opens a file and returns a pointer to the file stream. -> Option B
Quick Check:
fopen = open file pointer [OK]
Hint: fopen opens files, fclose closes them [OK]
Common Mistakes:
Confusing fopen with fclose
Thinking fopen reads or writes data
Not knowing fopen returns NULL on failure
2. Which of the following is the correct syntax to open a file named "data.txt" for writing in C?
easy
A. FILE *fp = fopen("data.txt", "w");
B. FILE *fp = fopen("data.txt", "r");
C. FILE *fp = open("data.txt", "w");
D. FILE fp = fopen("data.txt", "w");
Solution
Step 1: Identify correct fopen syntax
The correct syntax uses FILE * pointer and fopen with filename and mode as strings.
Step 2: Check mode for writing
Mode "w" opens the file for writing. "r" is for reading, and open is not a standard C function for files.
Final Answer:
FILE *fp = fopen("data.txt", "w"); -> Option A
Quick Check:
fopen + "w" = open for writing [OK]
Hint: Use FILE * and fopen with mode "w" to write [OK]
Common Mistakes:
Using open instead of fopen
Declaring FILE variable instead of pointer
Using wrong mode like "r" for writing
3. What will be the output of the following C code snippet?
FILE *fp = fopen("nonexistent.txt", "r");
if (fp == NULL) {
printf("File not found\n");
} else {
printf("File opened successfully\n");
fclose(fp);
}
medium
A. File opened successfully
B. No output
C. File not found
D. Compilation error
Solution
Step 1: Understand fopen with "r" mode on nonexistent file
Opening a file for reading with "r" mode returns NULL if the file does not exist.
Step 2: Check the if condition
The code checks if fp == NULL, then prints "File not found". Since file does not exist, this condition is true.
Final Answer:
File not found -> Option C
Quick Check:
fopen returns NULL if file missing [OK]
Hint: fopen returns NULL if file missing in "r" mode [OK]
Common Mistakes:
Assuming fopen creates file in "r" mode
Not checking for NULL before using file pointer
Expecting "File opened successfully" always
4. Identify the error in the following code snippet:
The fopen call with mode "w" is correct to open file for writing.
Step 2: Identify missing fclose
The code opens the file but never closes it with fclose, which can cause resource leaks.
Final Answer:
fclose is missing to close the file -> Option D
Quick Check:
Always fclose after fopen [OK]
Hint: Always close files with fclose after opening [OK]
Common Mistakes:
Ignoring fclose causes resource leaks
Not checking fopen return value
Thinking printf affects file handling
5. You want to append text to an existing file "log.txt" safely. Which code snippet correctly opens the file, checks for errors, writes "Hello\n", and closes the file?