Persistent data with files — modes, opening, reading, and writing in C
Grade XII • Computer Science ⏱️ ~20 minBrief Intro — File Handling
Normally, a program loses all its data as soon as it closes, but files allow you to save information permanently on a disk. C provides powerful tools to create, read, and write files, ensuring your data survives even after the computer is turned off.
In this activity, you will learn how to use the FILE pointer and various opening modes so you can manage persistent data in your own C programs.
Understanding the difference between text and binary files is the first step in choosing how to store your program's data.
Task 1: File & File Handling
A file is a container in computer storage devices used for storing data. File Handling is the storing of data in a file using a program. C programs store results and other data to a file, and can extract/fetch data from a file to work with it in the program.
Files persist program data on storage devices; file handling stores and fetches that data.
Task 2: Why Files?
Files preserve data after termination, avoid large data entry, and transfer between computers easily.
Task 3: Text vs Binary Files
Text files are normal .txt files: contents are plain text, easy to edit or delete, minimum effort to maintain, easily readable, provide the least security, and take bigger storage space. Binary files (mostly .bin) store data in binary form (0's and 1's): they hold a higher amount of data, are not readable easily, and provide better security.
Text = plain, readable, bigger, less secure. Binary = 0/1 form, more data, harder to read, better security.
To work with a file, you must follow a strict sequence: open it, check if it exists, perform your work, and then close it.
Task 4: Four File Operations
In C you can perform four major operations on files, either text or binary:
Create, open, close, read/write — the four major file operations.
Task 5: The FILE Pointer
When working with files, you need to declare a pointer of type FILE. This declaration is needed for communication between the file and the program:
FILE *fptr;
FILE *fptr; is the pointer that links the program to the file.
Task 6: Opening a File — fopen()
Opening a file is performed with the fopen() function defined in stdio.h:
ptr = fopen("fileopen", "mode");
For example, fopen("C:\program.txt", "w") creates the file (if absent) and opens it for writing; fopen("C:\oldprogram.bin", "rb") opens an existing binary file for reading. The writing mode lets you create and edit (overwrite) contents; the reading mode only lets you read — you cannot write into the file.
fopen("file","mode") opens or creates a file; "w" writes (overwrites), "r" reads only.
Click a dock pill to see what that mode does and what happens during the existence of the file.
Task 7: Opening Modes Table
Use the Explorer above. Key modes:
r / rb — open for reading; if the file does not exist, fopen() returns NULL.w / wb — open for writing; overwrites existing contents, or creates if absent.a / ab — open for append; data added to the end; creates if absent.r+ — open for reading and writing; returns NULL if the file does not exist.r-family modes return NULL when the file is missing; w/a families create it.
Task 8: Closing a File
The file (both text and binary) should be closed after reading/writing, using the fclose() function:
fclose(fptr);
Here, fptr is the file pointer associated with the file to be closed.
Always close an opened file with fclose(fptr); after use.
Reading and writing functions like fprintf and fscanf let your program communicate with files just like it does with the console.
Task 9: fprintf / fscanf
For reading and writing to a text file, we use fprintf() and fscanf(). They are just the file versions of printf() and scanf(). The only difference is that they expect a pointer to the structure FILE.
fprintf(fptr, ...) writes to the file; fscanf(fptr, ...) reads from it.
Run the notes' Example 1 → Example 2 flow with your own number.
Task 10: Write Example Trace
The write program: declare FILE *fptr;, fptr = fopen("program.txt","w");, guard with if(fptr == NULL) { printf("Error!"); exit(1); }, then scanf("%d",&num);, fprintf(fptr,"%d",num);, and fclose(fptr);. The integer you entered is stored in the text file.
Open with "w", check for NULL, write with fprintf, close with fclose.
Task 11: Read Example Trace
The read program: fopen("program.txt","r") (exit if NULL), fscanf(fptr,"%d", &num);, then printf("Value of n=%d", num);, and fclose(fptr);. It prints the integer stored earlier.
Open with "r", read with fscanf into &num, print the value, close the file.
Task 12: Read All + Binary Note
To read an entire file character by character, use fgetc(fp) in a while(1) loop, breaking when ch == EOF:
while ( 1 ) {
ch = fgetc(fp);
if ( ch == EOF )
break;
printf("%c", ch);
}
Other functions like fgetchar(), fputc() can be used in a similar way. For binary files, fread() and fwrite() are used for reading from and writing to a file on the disk.
fgetc reads one character at a time; EOF ends the loop. fread/fwrite handle binary files.
Ready to test your knowledge?
Take the Assessment →