A file pointer is a pointer that identifies and manages an opened file in C. It is used by the C Standard Library to perform file operations such as opening, reading, writing, and closing files.
- Maintains information about the current file position.
- Required for almost every file handling operation.
#include <stdio.h>
int main()
{
FILE *fp = fopen("file.txt", "r");
if (fp == NULL) {
printf("Unable to open file.");
return 1;
}
printf("File opened successfully.");
fclose(fp);
return 0;
}
Output
File opened successfully.Syntax
FILE *fp;
- FILE is a predefined structure declared in <stdio.h>.
- fp is a pointer that stores the address of an opened file.
Working
A file pointer manages access to a file by keeping track of its current position and file status during file operations.
- fopen() opens the file and returns a FILE * pointer.
- The file pointer is positioned based on the selected file mode ("r", "w", or "a").
- Each read or write operation automatically updates the current file position.
- fclose() closes the file and releases the associated resources.
File Pointer in Different File Modes
The initial position and behavior of a file pointer depend on the mode used to open the file. Understanding these modes helps determine where reading or writing begins.
1. Read Mode ("r")
When a file is opened in read mode, the file pointer is positioned at the beginning of the file.
Syntax
FILE *fp = fopen("file.txt", "r");
Behavior
- Starts at the beginning of the file.
- Moves forward after every read operation.
- Allows only reading from the file.
- Fails if the file does not exist.
2. Write Mode ("w")
When a file is opened in write mode, the file pointer is placed at the beginning of the file.
Syntax
FILE *fp = fopen("file.txt", "w");
Behavior
- Starts at the beginning of the file.
- Removes all existing contents of the file.
- Moves forward after every write operation.
- Creates the file if it does not exist.
3. Append Mode ("a")
When a file is opened in append mode, the file pointer is positioned at the end of the file.
Syntax
FILE *fp = fopen("file.txt", "a");
Behavior
- Starts at the end of the file.
- Preserves the existing file contents.
- Appends new data after the current contents.
- Creates the file if it does not exist.
File Pointer Position in Different Modes
| Mode | Initial Position | Allowed Operations |
|---|---|---|
| "r" | Beginning of the file | Read only |
| "w" | Beginning of the file | Write only |
| "a" | End of the file | Append only |
Purpose
A file pointer allows the operating system and the C runtime library to keep track of an opened file. It stores information such as:
- Current position within the file.
- File access mode.
- Status of the file stream.
- Error and end-of-file indicators.
Note: The internal structure of FILE is implementation-dependent and should not be accessed directly.
Limitations
Although file pointers simplify file handling, they have a few limitations.
- A file pointer becomes invalid after fclose() and cannot be used until the file is opened again.
- If fopen() fails (for example, the file does not exist in "r" mode), it returns NULL, so it must always be checked.
- The internal structure of FILE is implementation-dependent and should never be accessed or modified directly.
- Using an invalid or uninitialized file pointer can lead to undefined behavior or program crashes.