Concept of File in C Programming (Definition, Types & Examples) | File Handling in C Explained
Learn the concept of file in C programming — including definition, file types, file handling functions, and real examples using fopen(), fclose(), fprintf(), fscanf(), and more.
Introduction: What is a File in C Programming?
A file in C programming is a collection of data stored permanently on a storage device such as a hard disk.
- Unlike variables that store temporary data in RAM, files allow programs to store, retrieve, and manipulate data permanently — even after the program ends.
File handling in C enables you to perform operations like creating, reading, writing, appending, and closing files efficiently.
Need for File Handling in C
Without file handling, all program data would be lost once execution ends.
File handling helps:
- Store large amounts of data permanently.
- Retrieve and modify data anytime.
- Manage user information, logs, or configurations.
- Create backup and report systems.
Types of Files in C
C language supports two types of files:
- Text Files (.txt)
- Data stored in human-readable form (ASCII).Example: names, numbers, or words separated by spaces or new lines.Easy to open and edit with any text editor.
Hello
123
Welcome to C Programming- Binary Files (.bin)
- Data stored in binary format (0s and 1s).
- Faster to read/write and more memory-efficient.
- Not human-readable but suitable for complex data storage like images or records.
File Operations in C
| Operation | Description | Function Used |
|---|---|---|
| Create/Open a file | Opens or creates a file for I/O operations | fopen() |
| Read data | Reads data from a file | fscanf(), fread() |
| Write data | Writes data to a file | fprintf(), fwrite() |
| Close file | Closes the opened file | fclose() |
| Move pointer | Moves file position indicator | fseek(), rewind() |
Basic File Handling Example in C
#include <stdio.h>
int main() {
FILE *fp; // File pointer
fp = fopen("example.txt", "w"); // Open file in write mode
if (fp == NULL) {
printf("Error opening file!");
return 1;
}
fprintf(fp, "Hello, this is a file handling example in C.\n");
fclose(fp);
printf("Data written successfully!\n");
return 0;
}Explanation:
FILE *fpdeclares a file pointer.fopen()opens a file in a specific mode ("w"for write).fprintf()writes formatted data to the file.fclose()closes the file and releases resources.
Common File Handling Functions
| Function | Description | Example |
|---|---|---|
fopen() | Opens a file in different modes | fopen("data.txt", "r"); |
fclose() | Closes an opened file | fclose(fp); |
fprintf() | Writes formatted data to a file | fprintf(fp, "Name: %s", name); |
fscanf() | Reads formatted data from a file | fscanf(fp, "%d", &num); |
fgets() | Reads string from file | fgets(str, 100, fp); |
fputs() | Writes string to file | fputs("Hello", fp); |
fseek() | Moves file pointer to specific location | fseek(fp, 0, SEEK_SET); |
ftell() | Returns current position of file pointer | ftell(fp); |
rewind() | Resets file pointer to start | rewind(fp); |
File Modes in C
| Mode | Description |
|---|---|
"r" | Opens file for reading |
"w" | Opens file for writing (overwrites existing content) |
"a" | Opens file for appending |
"r+" | Opens file for reading and writing |
"w+" | Opens file for reading and writing (overwrites) |
"a+" | Opens file for reading and appending |
Why File Handling Matters
- Ensures data persistence after program termination.
- Facilitates data sharing between programs.
- Simplifies storage management for large datasets.
- Enables building real-world applications such as billing systems, databases, and logs.
Conclusion
The concept of file in C forms the foundation for data management and persistence in programming.
By mastering file operations like fopen(), fprintf(), fscanf(), and fclose(), developers can create efficient, real-world C applications that handle data smoothly.
