File Handling
1. Introduction to File Handling
In C programming, file handling is used to store data permanently in a file on a computer. Normally, when a program ends, all data stored in variables is lost. To avoid this problem, files are used to store data permanently so it can be used later.
Why File Handling is Important
-
Data can be stored permanently
-
Large amounts of data can be saved
-
Data can be reused later
-
Used in real-life applications like:
-
Student records
-
Employee details
-
Banking systems
-
Inventory management
-
C provides several library functions to work with files.
2. Types of Files in C
There are mainly two types of files in C:
1. Text Files
-
Data is stored in human-readable form
-
Easy to read and edit
-
Examples:
.txt,.c -
Uses characters and strings
Example:
2. Binary Files
-
Data is stored in binary (0s and 1s)
-
Faster and more secure
-
Not human-readable
-
Uses structures and raw data
Example:
3. File Pointer
To work with files in C, we use a file pointer.
Definition
A file pointer is a pointer of type FILE which points to a file.
Syntax
-
FILEis a structure defined instdio.h -
fpis the file pointer
4. Opening a File
To open a file, we use the fopen() function.
Syntax
Example
File Opening Modes
| Mode | Meaning |
|---|---|
"r" |
Read only |
"w" |
Write only |
"a" |
Append |
"r+" |
Read and write |
"w+" |
Write and read |
"a+" |
Append and read |
âš If the file does not exist:
-
"r"→ error -
"w"→ creates new file -
"a"→ creates new file
5. Closing a File
After working with a file, it must be closed using fclose().
Syntax
Importance of Closing a File
-
Saves data properly
-
Frees memory
-
Prevents data corruption
6. Writing Data to a File
1. Using fprintf()
Used to write formatted data to a file.
Syntax
Example Program
2. Using fputc()
Used to write one character at a time.
Syntax
3. Using fputs()
Used to write a string to a file.
Syntax
7. Reading Data from a File
1. Using fscanf()
Reads formatted data from a file.
Syntax
Example
2. Using fgetc()
Reads one character at a time.
Syntax
3. Using fgets()
Reads a line of text.
Syntax
8. End of File (EOF)
When reading a file, we need to know when the file ends. C uses EOF (End Of File).
Example
9. Binary File Handling
Binary files are used to store data in binary format.
Functions Used
-
fwrite() -
fread()
Writing to a Binary File
Syntax
Example
Reading from a Binary File
Syntax
10. File Handling Functions Summary
| Function | Purpose |
|---|---|
fopen() |
Open a file |
fclose() |
Close a file |
fprintf() |
Write formatted data |
fscanf() |
Read formatted data |
fputc() |
Write a character |
fgetc() |
Read a character |
fputs() |
Write a string |
fgets() |
Read a string |
fwrite() |
Write binary data |
fread() |
Read binary data |
11. Advantages of File Handling
-
Permanent data storage
-
Easy data retrieval
-
Useful for large data
-
Supports real-world applications
12. Conclusion
File handling in C is an important concept that allows programs to store and retrieve data permanently. By using file pointers and file functions, we can easily read and write data to files. Understanding file handling helps in developing practical and real-world applications.