📖 Scenario: You are creating a simple program to read data from a text file. Before you can read the data, you need to open the file safely and then close it when done.
🎯 Goal: Build a C program that opens a file called data.txt in read mode, checks if the file opened successfully, and then closes the file.
📋 What You'll Learn
Create a FILE* pointer variable named file.
Open the file data.txt in read mode using fopen.
Check if the file opened successfully by testing if file is not NULL.
Close the file using fclose.
Print "File opened and closed successfully." if the file opens, otherwise print "Failed to open file.".
💡 Why This Matters
🌍 Real World
Opening and closing files is a basic task in many programs that need to read or write data, such as reading configuration files or saving user data.
💼 Career
Understanding file handling is essential for software developers, especially those working with system programming, data processing, or embedded systems.
Progress0 / 4 steps
1
Create a FILE* pointer variable
Create a FILE* pointer variable called file and set it to NULL.
C
Hint
Use FILE *file = NULL; to declare the file pointer.
2
Open the file data.txt in read mode
Use fopen to open the file "data.txt" in read mode "r" and assign it to the variable file.
C
Hint
Use file = fopen("data.txt", "r"); to open the file.
3
Check if the file opened successfully
Write an if statement to check if file is not NULL. If it is not NULL, print "File opened and closed successfully.". Otherwise, print "Failed to open file.".
C
Hint
Use if (file != NULL) to check if the file opened.
4
Close the file
Inside the if block where file is not NULL, close the file using fclose(file);.
C
Hint
Use fclose(file); to close the file inside the if block.
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?