UNIT V
FILE PROCESSING
Introduction to files, using files in C, read data from files, Writing Data to files, Detecting the End of file, Error
Handling during file operations; Accepting Command line arguments, Function for selecting a record randomly,
Remove and renaming the File, Creating temporary file, memory allocation in C Programs: Dynamic memory
allocation, Preprocessor directives.
5.1 Introduction to files
A file is a collection of data stored on a secondary storage device like hard disk. Broadly speaking, a file is
basically used because real- life applications involve large amounts of data and in such applications the console-
oriented I/O operations pose two major problems:
• It becomes cumbersome and time-consuming to handle huge amount of data through terminals.
• When doing I/O using terminal, the entire data is lost when either the program is terminated or
computer is turned off. Therefore, it becomes necessary to store data on a permanent storage device
(the disks) and read whenever necessary, without destroying the data.
Streams in C
In C, the standard streams are termed as pre-connected input and output channels between a text terminal and
the program (when it begins execution). Therefore, stream is a logical interface to the devices that are connected
to the computer.
Stream is widely used as a logical interface to a file where a file can refer to a disk file, the computer screen,
keyboard, etc. Although files may differ in the form and capabilities, all streams are the same. The three standard
streams in C language are as follows:
• standard input (stdin)
• standard output (stdout) and
• standard error (stderr).
Standard input (stdin) Standard input is the stream from which the program receives its data using the read
operation.
Standard output (stdout) Standard output is the stream where a program writes its output data using the write
operation.
Standard error (stderr) Standard error is an output stream used by programs to report error messages.
A stream is linked to a file using an open operation and dissociated from a file using a close operation.
Buffer Associated with File Stream
When a stream linked to a disk file is created, a buffer is automatically created and associated with the stream.
A buffer is nothing but a block of memory that is used for temporary storage of data that has to be read from or
written to a file.
,Similarly, when reading data from a disk file, the data is read as a block from the file and written into the buffer.
The program reads data from the buffer. The creation and operation of the buffer is automatically handled by
the operating system.
Types of Files
In C, the types of files used can be broadly classified into two categories-ASCII text files and binary files.
ASCII Text Files
The file that contains ASCII codes of data like digits, alphabets and symbols is called text file (or) ASCII file.
Binary Files
The file that contains data in the form of bytes (0's and 1's) is called as binary file. Generally, the binary files
are compiled version of text files.
5.2 USING FILES IN C
To use files in C, we must follow the steps given below:
• declare a file pointer variable
• open the file
• process the file
• close the file
5.2.1 Declaring a File Pointer Variable
There can be a number of files on the disk. In order to access a particular file, you must specify the name of the
file that has to be used.
This is accomplished by using a file pointer variable that points to a structure FILE (defined in stdio.h). The
syntax for declaring a file pointer is
FILE *file_pointer_name;
For example, if we write
FILE *fp;
Then, fp is declared as a file pointer.
5.2.2 Opening a File
A file must first be opened before data can be read from it or written to it. In order to open a file and associate
it with a stream, the fopen() function is used. The prototype of fopen() can be given as
FILE *fopen(*file_name, mode);
Using the above prototype, the file whose pathname is the string pointed to by file_name is opened in the mode
specified using the mode. If successful, fopen() returns a pointer-to- structure and if it fails, it returns NULL.
File Name
Every file on the disk has a name known as the file name.
In C, the fopen() may contain the path information instead of specifying the filename. The path gives
information about the location of the file on the disk. If a filename is specified without a path, it is assumed that
the file is located in the current working directory. For example, if a file named Student. DAT is located on D
drive in directory BCA, then the path of the file can be specified by writing.
D:\BCA\Student. DAT
In C, a backslash character has a special meaning with respect to escape sequences when placed in a string. So
in order to represent a backslash character in a C program, you must precede it with another backslash. Hence,
the above path will be specified as given below in the C program.
D:\\BCA\\Student. DAT
File Mode
The second argument in the fopen() is the mode. Mode conveys to C the type of processing that will be done
with the file. The different modes in which a file can be opened for processing are given in Table.
Look at the code given below which opens a file using the fopen().
FILE *fp;
fp = fopen("Student.DAT", "r");
, So it is recommended to check whether the file was successfully opened before actually using the file. The
fopen() can fail to open the specified file under certain conditions that are listed below:
• opening a file that is not ready for use
• opening a file that is specified to be on a non-existent directory/drive
• opening a non-existent file for reading
• opening a file to which access is not permitted
if (fp==NULL)
{
printf("\n The file could not be opened");
exit (1);
}
FILE PROCESSING
Introduction to files, using files in C, read data from files, Writing Data to files, Detecting the End of file, Error
Handling during file operations; Accepting Command line arguments, Function for selecting a record randomly,
Remove and renaming the File, Creating temporary file, memory allocation in C Programs: Dynamic memory
allocation, Preprocessor directives.
5.1 Introduction to files
A file is a collection of data stored on a secondary storage device like hard disk. Broadly speaking, a file is
basically used because real- life applications involve large amounts of data and in such applications the console-
oriented I/O operations pose two major problems:
• It becomes cumbersome and time-consuming to handle huge amount of data through terminals.
• When doing I/O using terminal, the entire data is lost when either the program is terminated or
computer is turned off. Therefore, it becomes necessary to store data on a permanent storage device
(the disks) and read whenever necessary, without destroying the data.
Streams in C
In C, the standard streams are termed as pre-connected input and output channels between a text terminal and
the program (when it begins execution). Therefore, stream is a logical interface to the devices that are connected
to the computer.
Stream is widely used as a logical interface to a file where a file can refer to a disk file, the computer screen,
keyboard, etc. Although files may differ in the form and capabilities, all streams are the same. The three standard
streams in C language are as follows:
• standard input (stdin)
• standard output (stdout) and
• standard error (stderr).
Standard input (stdin) Standard input is the stream from which the program receives its data using the read
operation.
Standard output (stdout) Standard output is the stream where a program writes its output data using the write
operation.
Standard error (stderr) Standard error is an output stream used by programs to report error messages.
A stream is linked to a file using an open operation and dissociated from a file using a close operation.
Buffer Associated with File Stream
When a stream linked to a disk file is created, a buffer is automatically created and associated with the stream.
A buffer is nothing but a block of memory that is used for temporary storage of data that has to be read from or
written to a file.
,Similarly, when reading data from a disk file, the data is read as a block from the file and written into the buffer.
The program reads data from the buffer. The creation and operation of the buffer is automatically handled by
the operating system.
Types of Files
In C, the types of files used can be broadly classified into two categories-ASCII text files and binary files.
ASCII Text Files
The file that contains ASCII codes of data like digits, alphabets and symbols is called text file (or) ASCII file.
Binary Files
The file that contains data in the form of bytes (0's and 1's) is called as binary file. Generally, the binary files
are compiled version of text files.
5.2 USING FILES IN C
To use files in C, we must follow the steps given below:
• declare a file pointer variable
• open the file
• process the file
• close the file
5.2.1 Declaring a File Pointer Variable
There can be a number of files on the disk. In order to access a particular file, you must specify the name of the
file that has to be used.
This is accomplished by using a file pointer variable that points to a structure FILE (defined in stdio.h). The
syntax for declaring a file pointer is
FILE *file_pointer_name;
For example, if we write
FILE *fp;
Then, fp is declared as a file pointer.
5.2.2 Opening a File
A file must first be opened before data can be read from it or written to it. In order to open a file and associate
it with a stream, the fopen() function is used. The prototype of fopen() can be given as
FILE *fopen(*file_name, mode);
Using the above prototype, the file whose pathname is the string pointed to by file_name is opened in the mode
specified using the mode. If successful, fopen() returns a pointer-to- structure and if it fails, it returns NULL.
File Name
Every file on the disk has a name known as the file name.
In C, the fopen() may contain the path information instead of specifying the filename. The path gives
information about the location of the file on the disk. If a filename is specified without a path, it is assumed that
the file is located in the current working directory. For example, if a file named Student. DAT is located on D
drive in directory BCA, then the path of the file can be specified by writing.
D:\BCA\Student. DAT
In C, a backslash character has a special meaning with respect to escape sequences when placed in a string. So
in order to represent a backslash character in a C program, you must precede it with another backslash. Hence,
the above path will be specified as given below in the C program.
D:\\BCA\\Student. DAT
File Mode
The second argument in the fopen() is the mode. Mode conveys to C the type of processing that will be done
with the file. The different modes in which a file can be opened for processing are given in Table.
Look at the code given below which opens a file using the fopen().
FILE *fp;
fp = fopen("Student.DAT", "r");
, So it is recommended to check whether the file was successfully opened before actually using the file. The
fopen() can fail to open the specified file under certain conditions that are listed below:
• opening a file that is not ready for use
• opening a file that is specified to be on a non-existent directory/drive
• opening a non-existent file for reading
• opening a file to which access is not permitted
if (fp==NULL)
{
printf("\n The file could not be opened");
exit (1);
}