UNIT-1
INTRODUCTION
File Handling:
File handling in C is the process in which we create, open, read, write, and close
operations on a file. C language provides different functions such as fopen(),
fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many
different C file operations in our program.
Types of Files in C
A file can be classified into two types based on the way the file stores the data.
They are as follows:
1. Text Files
2. Binary Files
1. Text Files
A text file contains data in the form of ASCII characters and is generally
used to store a stream of characters.
Each line in a text file ends with a new line character (‘\n’).
It can be read or written by any text editor.
They are generally stored with .txt file extension.
Text files can also be used to store the source code.
SHIVAKUMAR C.R , A.G.B First Grade College, Davangere 1
, Data structure using-C
2. Binary Files
A binary file contains data in binary form (i.e. 0’s and 1’s) instead of ASCII
characters. They contain data that is stored in a similar manner to how it is
stored in the main memory.
The binary files can be created only from within a program and their
contents can only be read by a program.
More secure as they are not easily readable.
They are generally stored with .bin file extension.
Opening a file
Before we perform any operations on a file, we need to open it. We do this
by using a file pointer. The type FILE defined in stdio.h allows us to define
a file pointer. Then you use the function fopen() for opening a file. Once
this is done one can read or write to the file using the fread() or fwrite()
functions, respectively. The fclose() function is used to explicitly close any
opened file.
Taking the preceding statements into account let us look at the following
example program :
fopen()
#include <stdio.h>
main ()
{
FILE *fp;
fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("File does not exist,
please check!\n");
}
SHIVAKUMAR C.R , A.G.B First Grade College, Davangere 2
, Data structure using-C
fclose(fp);}
Let us first discuss fopen(). This function accepts two arguments as strings. The
first argument denotes the name of the file to be opened and the second signifies
the mode in which the file is to be opened. The second argument can be any of
the following:
File Mode Description
r Open a text file for reading
w Create a text file for writing, if it exists, it is overwritten.
a Open a text file and append text to the end of the file.
rb Open a binary file for reading
wb Create a binary file for writing, if it exists, it is overwritten.
ab Open a binary file and append data to the end of the file.
fclose()
The fclose() function is used for closing opened files. The only argument it
accepts is the file pointer.
If a program terminates, it automatically closes all opened files. But it is a good
programming habit to close any file once it is no longer needed. This helps in
better utilization of system resources, and is very useful when you are working
on numerous files simultaneously. Some operating systems place a limit on the
number of files that can be open at any given point in time.
SHIVAKUMAR C.R , A.G.B First Grade College, Davangere 3
, Data structure using-C
fscanf() and fprintf()
The functions fprintf() and fscanf() are similar to printf() and scanf() except that
these functions operate on files and require one additional and first argument to
be a file pointer.
Example:
#include <stdio.h>
main ()
{
FILE *fp;
float total;
fp = fopen("data.txt", "w+");
if (fp == NULL) {
printf("data.txt does not exist, please check!\n");
exit (1);
}
fprintf(fp, 100);
fscanf(fp, "%f", &total);
fclose(fp);
printf("Value of total is %f\n", total);
}
getc() and putc()
The functions getc() and putc() are equivalent to getchar() and putchar()
functions on Input and Output, except that these functions require an argument
which is the file pointer. Function getc() reads a single character from the file
which has previously been opened using a function like fopen(). Function putc()
SHIVAKUMAR C.R , A.G.B First Grade College, Davangere 4