Posted by yanib on 2025-03-16 08:35:57 | Last Updated by yanib on 2025-07-19 01:34:30
Share: Facebook | Twitter | Whatsapp | Linkedin Visits: 224
File Handling in C refers to the process of creating, reading, writing, and closing files from within a C program. Files are used to store data permanently (as opposed to variables that exist only while the program is running). In C, file handling is done using the standard library functions defined in the <stdio.h>
header file. Through file handling, you can interact with files on the computer’s storage, perform operations like writing data, reading data, and managing file content.
When opening a file in C using the fopen()
function, you specify a mode to indicate the type of operation you intend to perform on the file. Here are the common modes used in C for file handling:
"r"
- Read Modefopen()
call will return NULL
.
"w"
- Write Mode
"a"
- Append Mode
"r+"
- Read/Write Modefopen()
will return NULL
. This mode allows you to read from and write to the file.
"w+"
- Write/Read Mode
"a+"
- Append/Read Mode
"b"
- Binary Mode
Mode | Description |
---|---|
"r" | Opens the file for reading. The file must exist. |
"w" | Opens the file for writing. If the file exists, it is overwritten. |
"a" | Opens the file for appending. If the file exists, new data is added at the end. |
"r+" | Opens the file for reading and writing. The file must exist. |
"w+" | Opens the file for writing and reading. The file is overwritten if it exists. |
"a+" | Opens the file for reading and appending. Data is added at the end. |
"rb" , "wb" , "ab" | For binary files (read, write, append in binary mode). |
fopen()
with modes:
This example opens a file for writing, writes to it, and then opens it for reading and prints the contents.