In this article, we explain File Handling in C, its types, functions, and practical examples. File Handling is a process in which data is stored in a file using a program so that it can be accessed and reused even after the program ends.
File Handling in C
What is a File?
A file is a collection of bytes stored on a secondary storage device such as a hard disk. Files are used to store data permanently, unlike variables, arrays, structures, or unions, which are temporary and lost when a program ends.
Types of Files:
- Text File:
- Stores data in plain text format (.txt).
- Can be created using simple text editors like Notepad.
- Easily readable and editable.
- Binary File:
- Stores data in binary format (.bin).
- Can store large amounts of data efficiently.
- Not human-readable, suitable for storing complex data like images, audio, or executable files.
Why Use File Handling?
- To store program data permanently.
- To reuse data without running the program again.
- To transfer data between computers.
- To handle large amounts of data efficiently.
File Handling in C
In C, a file is treated as a container to store data. With File Handling, we can:
- Create a new file.
- Open a file.
- Read data from a file.
- Write data to a file.
- Delete a file.
- Close a file.
File Handling Functions
| Sr. No. | Function | Description |
| 1 | fopen() | Opens a file. |
| 2 | fprintf() | Writes data to a file. |
| 3 | fscanf() | Reads data from a file. |
| 4 | fputc() | Writes a single character to a file. |
| 5 | fgetc() | Reads a single character from a file. |
| 6 | fclose() | Closes a file. |
| 7 | fseek() | Moves the file pointer to a specific location. |
| 8 | fputw() | Writes an integer to a file. |
| 9 | fgetw() | Reads an integer from a file. |
| 10 | ftell() | Returns the current position of the file pointer. |
| 11 | rewind() | Sets the file pointer to the beginning of the file. |
Opening a File – fopen()
Syntax:
FILE *file_pointer;
file_pointer = fopen(“file_name”, “mode”);
- file_pointer: Pointer of type FILE.
- file_name: Name of the file with extension (e.g., .txt, .doc).
- mode: Determines how the file is opened.
File Modes:
| Mode | Description |
| r | Read mode |
| w | Write mode |
| a | Append mode |
| r+ | Read and write mode |
| w+ | Write and read mode |
| a+ | Append and read mode |
| rb | Read binary file |
| wb | Write binary file |
| ab | Append binary file |
| rb+ | Read/write binary file |
| wb+ | Write/read binary file |
| ab+ | Append/read binary file |
Example:
#include <stdio.h>
int main() {
FILE *fptr;
fptr = fopen(“sample.txt”, “w”); // Open file in write mode
fclose(fptr); // Close the file
return 0;
}
Reading from a File
Functions to read from a file:
- fscanf()
- fgets()
- fgetc()
Example using fscanf():
#include <stdio.h>
int main() {
FILE *fptr;
char name[50];
fptr = fopen(“sample.txt”, “r”);
fscanf(fptr, “%s”, name);
printf(“Read from file: %s\n”, name);
fclose(fptr);
return 0;
}
Writing to a File
Functions to write to a file:
- fprintf() – writes formatted data.
- fputs() – writes a string.
- fputc() – writes a single character.
Example using fprintf():
#include <stdio.h>
int main() {
FILE *fptr;
fptr = fopen(“sample.txt”, “w”);
fprintf(fptr, “Hello, File Handling in C!”);
fclose(fptr);
return 0;
}
Advantages of File Handling
- Permanent Storage: Data is saved even after program ends.
- Large Data Management: Can handle large volumes of data efficiently.
- Quick Access: File functions allow random access to data.
- Portability: Files can be transferred to different computers easily.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining