File Handling in C
In programming, we may require some specific input data to be generated
several numbers of times. Sometimes, it is not enough to only display the
data on the console. The data to be displayed may be very large, and only a
limited amount of data can be displayed on the console, and since the
memory is volatile, it is impossible to recover the programmatically generated
data again and again. However, if we need to do so, we may store it onto the
local file system which is volatile and can be accessed every time. Here,
comes the need of file handling in C.
File handling in C enables us to create, update, read, and delete the files
stored on the local file system through our C program. The following
operations can be performed on a file.
o Creation of the new file
o Opening an existing file
o Reading from the file
o Writing to the file
o Deleting the file
Functions for file handling
There are many functions in the C library to open, read, write, search and
close the file. A list of file functions are given below:
1 fopen() opens new or existing file
2 fprintf() write data into the file
3 fscanf() reads data from the file
writes a character into the
4 fputc()
file
reads a character from
5 fgetc()
file
6 fclose()
closes the file
sets the file pointer to
7 fseek()
given position
8 fputw() writes an integer to file
9 fgetw() reads an integer from file
10 ftell() returns current position
sets the file pointer to the
11 rewind()
beginning of the file
Opening File: fopen()
We must open a file before it can be read, write, or update. The fopen()
function is used to open a file. The syntax of the fopen() is given below.
1. FILE *fopen( const char * filename, const char * mode );
The fopen() function accepts two parameters:
o The file name (string). If the file is stored at some specific location,
then we must mention the path at which the file is stored. For
example, a file name can be like "c://some_folder/some_file.ext".
o The mode in which the file is to be opened. It is a string.
The fopen function works in the following way.
o Firstly, It searches the file to be opened.
o Then, it loads the file from the disk and place it into the buffer. The
buffer is used to provide efficiency for the read operations.
o It sets up a character pointer which points to the first character of
the file.
Consider the following example which opens a file in write mode.
Closing File: fclose()
The fclose() function is used to close a file. The file must be closed after
performing all the operations on it. The syntax of fclose() function is given
below:
int fclose( FILE *fp );
Writing File : fprintf() function
The fprintf() function is used to write set of characters into file. It sends
formatted output to a stream.
Syntax:
int fprintf(FILE *stream, const char *format [, argument, ...])
Reading File : fscanf() function
The fscanf() function is used to read set of characters from file. It reads a
word from the file and returns EOF at the end of file.
Syntax:
int fscanf(FILE *stream, const char *format [, argument, ...])
Writing File : fputc() function
The fputc() function is used to write a single character into file. It outputs a
character to a stream.
Syntax:
int fputc(int c, FILE *stream)
Reading File : fgetc() function
The fgetc() function returns a single character from the file. It gets a character
from the stream. It returns EOF at the end of file.
Syntax:
int fgetc(FILE *stream)
fputs() and fgets()
The fputs() and fgets() in C programming are used to write and read string
from stream. Let's see examples of writing and reading file using fgets() and
fgets() functions.
Writing File : fputs() function
The fputs() function writes a line of characters into file. It outputs string to a
stream.
Syntax:
int fputs(const char *s, FILE *stream)
Reading File : fgets() function
The fgets() function reads a line of characters from file. It gets string from a
stream.
Syntax:
char* fgets(char *s, int n, FILE *stream)
C fseek() function
The fseek() function is used to set the file pointer to the specified offset. It is
used to write data into file at desired location.
Syntax:
int fseek(FILE *stream, long int offset, int whence)
C rewind() function
The rewind() function sets the file pointer at the beginning of the stream. It is
useful if you have to use stream many times.
Syntax:
void rewind(FILE *stream)
C ftell() function
The ftell() function returns the current file position of the specified stream. We
can use ftell() function to get the total size of a file after moving file pointer at
the end of file. We can use SEEK_END constant to move the file pointer at
the end of file.
Syntax:
long int ftell(FILE *stream)