The FILE pointer type
What you'll learn: You will understand how C uses a special "address book" variable to keep track of a file's location and status on your hard drive.
Your program's library card
Imagine you are visiting a massive library with millions of books. You don't just grab every book at once; instead, you go to the front desk, and the librarian hands you a small slip of paper. This slip tells you exactly which shelf the book is on, which page you are currently reading, and whether you are allowed to write notes in the margins.
In C, the FILE type is that slip of paper. Because files live on your hard drive—far away from your computer's fast-acting RAM—your program cannot interact with them directly like a normal variable. Instead, C uses a special structure called FILE (always written in all caps) to manage the connection between your code and the data on the disk.
Meet the Pointer
You will almost never see FILE on its own. Instead, you will see it as a pointer: FILE *.
Think of FILE * as a "handle." When you open a file, the operating system gives you a pointer to a FILE structure. This structure acts as a bookmark. It remembers where the file is and, most importantly, where your "cursor" is inside that file. If you read ten words, the FILE pointer updates internally so that the next time you ask for data, it knows to start at the eleventh word.
How it looks in code
You don't need to know the complex guts of what is inside the FILE structure; you just need to create a place to store its address.
#include <stdio.h>
int main() {
// 1. Create a "handle" for a file
FILE *myFilePtr;
// 2. Point it to a specific file (opening it)
myFilePtr = fopen("notes.txt", "r");
// 3. If the pointer isn't NULL, we successfully connected!
if (myFilePtr != NULL) {
// We use myFilePtr here to read or write
printf("File opened successfully.");
// Always let go of the handle when done
fclose(myFilePtr);
}
return 0;
}
Why it matters
By using a FILE pointer, C keeps your program organized. You can have ten different files open at once, each with its own FILE * variable. Each pointer acts as an independent bookmark, ensuring that your program never gets confused about which file it is reading or where it left off.
Key takeaway
A FILE * is a pointer to a structure that manages the connection between your program and a file on the disk.