File pointers help your program find and work with files on your computer. They keep track of where you are inside a file.
File pointers in C
Introduction
Syntax
C
FILE *pointer_name; pointer_name = fopen("filename", "mode");
FILE * is a special type that points to a file.
fopen opens the file and returns the pointer.
Examples
data.txt for reading and save the pointer in fp.C
FILE *fp; fp = fopen("data.txt", "r");
output.txt for writing. If the file does not exist, it will be created.C
FILE *fp = fopen("output.txt", "w");
log.txt for appending. New data will be added at the end.C
FILE *fp = fopen("log.txt", "a");
Sample Program
This program creates a file called example.txt, writes a message, closes it, then reopens it to read and print the message.
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 pointers!\n"); fclose(fp); fp = fopen("example.txt", "r"); if (fp == NULL) { printf("Failed to open file for reading.\n"); return 1; } char buffer[100]; if (fgets(buffer, sizeof(buffer), fp) != NULL) { printf("Read from file: %s", buffer); } fclose(fp); return 0; }
Important Notes
Always check if fopen returns NULL to avoid errors if the file can't be opened.
Use fclose to close files and free resources when done.
File pointers remember where you are in the file for reading or writing.
Summary
File pointers let your program open and work with files.
You use fopen to open a file and get a pointer.
Always close files with fclose when finished.
Practice
1. What does a file pointer in C represent?
easy
Solution
Step 1: Understand what a file pointer is
A file pointer is a special pointer that points to a file stream opened by the program.Step 2: Differentiate from other options
It is not the file contents or a function, but a reference to the file for reading or writing.Final Answer:
A reference to a file opened in the program -> Option BQuick Check:
File pointer = Reference to opened file [OK]
Hint: File pointer points to file stream, not contents [OK]
Common Mistakes:
- Thinking file pointer stores file data
- Confusing file pointer with file reading function
- Assuming file pointer is a file format
2. Which of the following is the correct way to open a file for reading in C?
easy
Solution
Step 1: Recall fopen syntax
The fopen function returns a pointer to FILE, so the variable must be FILE *fp.Step 2: Check each option
FILE *fp = fopen("file.txt", "r"); correctly declares FILE *fp and calls fopen with filename and mode "r".Final Answer:
FILE *fp = fopen("file.txt", "r"); -> Option AQuick Check:
Correct fopen syntax = FILE *fp = fopen("file.txt", "r"); [OK]
Hint: fopen returns FILE pointer, so use FILE * variable [OK]
Common Mistakes:
- Declaring FILE variable instead of pointer
- Using open() instead of fopen()
- Incorrect order of fopen parameters
3. What will be the output of this C code snippet?
FILE *fp = fopen("test.txt", "w");
fprintf(fp, "Hello\n");
fclose(fp);
fp = fopen("test.txt", "r");
char buffer[10];
fgets(buffer, 10, fp);
printf("%s", buffer);
fclose(fp);medium
Solution
Step 1: Write and close file
The code opens test.txt for writing, writes "Hello\n", then closes the file.Step 2: Read and print file content
It reopens test.txt for reading, reads a line into buffer, then prints it. fgets reads until newline or buffer limit.Final Answer:
Hello (printed as Hello with newline) -> Option DQuick Check:
File write/read prints "Hello" [OK]
Hint: fgets reads line including newline, printf prints it [OK]
Common Mistakes:
- Expecting literal \n printed
- Confusing fprintf and printf output
- Forgetting to close file before reopening
4. Identify the error in this code snippet:
FILE *fp;
fp = fopen("data.txt", "r");
char c = fgetc(fp);
while (c != EOF) {
putchar(c);
c = fgetc(fp);
}
fclose(fp);medium
Solution
Step 1: Check fgetc return type
fgetc returns int to allow EOF (-1) detection, storing in char loses EOF info.Step 2: Understand EOF check failure
Comparing char c to EOF fails because c cannot hold EOF value properly, causing infinite loop or wrong output.Final Answer:
fgetc returns int, not char, causing wrong EOF check -> Option AQuick Check:
Use int for fgetc result to detect EOF [OK]
Hint: Store fgetc result in int to detect EOF correctly [OK]
Common Mistakes:
- Using char instead of int for fgetc result
- Ignoring EOF check causing infinite loops
- Assuming putchar can't print chars
5. You want to append text "World" to an existing file "hello.txt" without deleting its content. Which is the correct way to open the file?
hard
Solution
Step 1: Understand file modes
"a" mode opens file for appending, preserving existing content and writing at the end.Step 2: Compare other modes
"w" overwrites file, "r" is read-only, "r+" allows read/write but does not append automatically.Final Answer:
FILE *fp = fopen("hello.txt", "a"); -> Option CQuick Check:
Append mode = "a" [OK]
Hint: Use "a" mode to add without erasing file [OK]
Common Mistakes:
- Using "w" which erases file content
- Using "r" which is read-only
- Confusing "r+" with append mode
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
