We open files to read or write data. Closing files saves changes and frees resources.
Opening and closing files
Introduction
Syntax
C
FILE *file_pointer = fopen("filename.txt", "mode"); // work with the file fclose(file_pointer);
"filename.txt" is the name of the file you want to open.
"mode" is how you want to use the file: "r" for reading, "w" for writing, "a" for appending.
Examples
C
FILE *fp = fopen("data.txt", "r"); // read from file fclose(fp);
C
FILE *fp = fopen("log.txt", "w"); // write to file fclose(fp);
C
FILE *fp = fopen("notes.txt", "a"); // add data to file fclose(fp);
Sample Program
This program opens a file named example.txt for writing, writes a message, closes the file, and prints a confirmation.
C
#include <stdio.h> int main() { FILE *fp = fopen("example.txt", "w"); if (fp == NULL) { printf("Failed to open file.\n"); return 1; } fprintf(fp, "Hello, file!\n"); fclose(fp); printf("File written and closed successfully.\n"); return 0; }
Important Notes
Always check if fopen returns NULL to avoid errors when opening files.
Closing files with fclose is important to save data and free system resources.
Summary
Use fopen to open files with a mode like "r", "w", or "a".
Always close files with fclose after finishing work.
Check if fopen returns NULL to handle errors safely.
Practice
1. What does the
fopen function do in C programming?easy
Solution
Step 1: Understand the purpose of fopen
Thefopenfunction 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 likefcloseclose files,freadreads data, andfwritewrites data. Onlyfopenopens files.Final Answer:
It opens a file and returns a pointer to the file stream. -> Option BQuick 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
Solution
Step 1: Identify correct fopen syntax
The correct syntax usesFILE *pointer andfopenwith filename and mode as strings.Step 2: Check mode for writing
Mode "w" opens the file for writing. "r" is for reading, andopenis not a standard C function for files.Final Answer:
FILE *fp = fopen("data.txt", "w"); -> Option AQuick 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
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 iffp == NULL, then prints "File not found". Since file does not exist, this condition is true.Final Answer:
File not found -> Option CQuick 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:
FILE *fp;
fp = fopen("file.txt", "w");
printf("File opened\n");
// Missing fclose heremedium
Solution
Step 1: Check fopen usage and mode
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 DQuick 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?
hard
Solution
Step 1: Choose correct mode for appending
Mode "a" opens file for appending without erasing existing content.Step 2: Check for NULL and handle errors
Always check if fopen returns NULL before writing. Use perror to show error if NULL.Step 3: Write and close file properly
Use fprintf to write text and fclose to close file to avoid leaks.Final Answer:
FILE *fp = fopen("log.txt", "a"); if (fp == NULL) { perror("Error opening file"); } else { fprintf(fp, "Hello\n"); fclose(fp); } -> Option AQuick Check:
Append mode + check NULL + fclose = safe file write [OK]
Hint: Use "a" mode, check NULL, fclose after writing [OK]
Common Mistakes:
- Using "w" mode erases file instead of appending
- Not checking fopen return value before writing
- Forgetting to close file with fclose
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
