
File handling in C language is the process of storing, updating, retrieving, deleting data from a file using a C program.
A file, as we all know, is a container for information. The files are kept on a secondary storage device. The file can keep the information forever if the secondary storage device is protected.
File handling in C allows us to use C program for creating, updating, reading, and deleting files. On a file, you can perform the following operations.
- New file creation
- Open existing file
- Read from file
- Write to file
- Delete file
Table of Contents
Types of files to process in C program
- Text files
- Binary Files
What is difference between text and binary files? Using a basic text editor, we create a text file with the .txt extension. Binary files hold data and information in a binary format of 0s and 1s. The application creates binary files with the.bin extension, which substitutes for the lack of text files in the program.
File handling Functions in c with examples
To open, read, write more data, create a new file, close or delete a file, search for a file, and so on, the C language offers us with a number of functions.
# | Functions | Description |
---|---|---|
1 | fopen() | Open a file, either an existing one or a new one. |
2 | fprintf() | Adding data to a file that is already open. |
3 | fscanf() | Reads data from the file. |
4 | fputc() | Writes any character into the file. |
6 | fgetc() | Reads any character from the file. |
7 | fclose() | Closing a opened file. |
8 | fseek() | This function is used to move the file pointer to the desired location. |
9 | fputw() | Writes an integer value to file. |
10 | fgetw() | Reads an integer value to file. |
11 | ftell() | Checks the file’s current status. |
12 | rewind() | Sets a desired file pointer to the start of the file. |
Opening a File in the Program – File: fopen()
To read, write, or update the file, we must first open it. The fopen() method is used to open a file in a C program.
FILE *fopen( const char * <file-name>, const char * mode ); fopen("E:\\geekcer\\tfile.txt","w"); fopen("E:\\geekcer\\bfile.bin","rb");
How to create a file in C?
To create a file in C you need to specify the file name and opening mode in fopen() function. The file is created and then opened if the file does not exist. f a file already available on the system, this fopen() method opens file directly.
FILE *file; file = fopen("<file-name>", "<opening-mode>");
File opening modes in C
Let’s have a look at some additional C program opening modes:
# | Model | Description |
---|---|---|
1 | r | Opens a text file for reading only. |
2 | w | Opens a text file for writing. |
3 | r+ | Allows you to read and write to a text file. |
4 | w+ | Allows you to read and write to a text file. |
5 | a | Opening a file to append the content. |
6 | a+ | Opening a file to append and reading data. |
7 | rb | Opening a binary file in read mode. |
8 | rb+ | Opening a binary file in read and write mode. |
9 | wb | Opening a binary file in write mode. |
10 | wb+ | Opening a binary file in reading and writing mode. |
11 | ab | Opening a binary file in append mode. |
12 | ab+ | Opening a binary file in reading and append mode. |
Program to open a file in read mode
FILE * fp; fp = fopen("<file-name>", "r");
#include
int main() {
FILE * fp;
fp = fopen("file.txt", "r");
if (fp == NULL) {
printf("File is not available, please check!\n");
}
fclose(fp);
return 0;
}
Write a program to display the contents of a file on the screen
#include
int main() {
FILE * fp;
int c;
fp = fopen("file.txt", "r");
c = getc(fp);
while (c != EOF) {
putchar(c);
c = getc(fp);
}
fclose(fp);
return 0;
}
Output: Hello, Welcome to C Programming Language.
C program to open a file in write mode
FILE * fp; fp = fopen("<file-name>", "w");
#include
int main() {
FILE * file;
/* Opening the file in write mode */
file = fopen("file.txt", "w");
/* Write string to opened file */
fprintf(file, "%s", "Example of writing content into file.");
/* Closing the file */
fclose(file);
return 0;
}
C program to open a file in append mode
FILE * fp; fp = fopen("<file-name>", "a");
#include
int main() {
FILE * fp;
fp = fopen("file.txt", "a");
fprintf(fp, "%s", "The appended content.");
fclose(fp);
return 0;
}