File handling is an essential feature of the C programming language that allows programs to store data permanently. Unlike variables, which lose their values when a program ends, files preserve information for future use.
Using file handling, applications can create files, write information, read stored data, update records, and process large amounts of information efficiently. File handling is widely used in student management systems, banking applications, inventory systems, payroll software, and many other business applications.
Learning Objectives
Understand file handling in C
Open and close files
Write data to files
Read data from files
Append data to existing files
Perform random file access
Understand command-line arguments
Why File Handling?
Normally, variables store data only while a program is running.
Files allow data to remain available even after the program is closed.
Common uses include:
Student records
Employee information
Inventory management
Banking transactions
Customer databases
Application logs
Opening a File
Files are opened using the fopen() function.
Syntax:
FILE *fp;
fp = fopen("student.txt","w");
Common file modes:
Mode | Description |
"r" | Read |
"w" | Write (creates a new file or overwrites an existing one) |
"a" | Append |
"r+" | Read and write |
"w+" | Read and write (overwrites existing file) |
"a+" | Read and append |
Writing to a File
Example:
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("student.txt","w");
fprintf(fp,"Welcome to Team Academy");
fclose(fp);
printf("Data written successfully.");
return 0;
}
Output
Data written successfully.
A file named student.txt is created with the following content:
Welcome to Team Academy
Reading from a File
Example:
#include <stdio.h>
int main()
{
FILE *fp;
char text[100];
fp = fopen("student.txt","r");
fgets(text,100,fp);
printf("%s",text);
fclose(fp);
return 0;
}
Output
Welcome to Team Academy
Appending Data
Appending adds new information without deleting existing content.
Example:
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("student.txt","a");
fprintf(fp,"\nLearning File Handling");
fclose(fp);
return 0;
}
The file now contains:
Welcome to Team Academy
Learning File Handling
Closing a File
Always close files after use.
Example:
fclose(fp);
Closing a file releases system resources and ensures data is saved correctly.
Random File Access
Random access allows programs to jump directly to a specific location within a file.
Common functions:
fseek()
ftell()
rewind()
Example:
fseek(fp,0,SEEK_SET);
This moves the file pointer to the beginning of the file.
Command-Line Arguments
Command-line arguments allow users to provide input when running a program.
Example:
int main(int argc,char *argv[])
Where:
argc → Number of arguments
argv → Array of argument strings
These are commonly used for processing filenames and user-supplied input.
Best Practices
Always check whether a file opened successfully.
Close files using fclose().
Use the correct file mode.
Validate file operations before reading or writing.
Handle file errors appropriately.
Common Mistakes
Forgetting to close files.
Opening a file with the wrong mode.
Reading from a file before opening it.
Ignoring NULL pointers returned by fopen().
Still need help?
Contact us