Unit VI-Part1
Unit VI-Part1
for Problem
Solving
(CST151)
UNIT-6: File handling
1
Syllabus
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.
Step 2: read data from the file or write data to the file using the stream
◦ input/output commands
FILE *file_pointer_name;
FILE *fp;
An error will be generated if you use the filename to access a file rather than
the file pointer.
Opening a File
A file must be first 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(const char *file_name, const char *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.
Mode Meaning
r Open a text file for reading only. If the file doesn’t exist, it returns
The fclose() function not only closes the file but also flushed all the buffers that are
maintained for that file.
If you do not close a file after using it, the system closes it automatically when the program
exits. However, since there is a limit on the number of files which can be open
simultaneously; the programmer must close a file when it has been used. The prototype of
the fclose() function can be given as,
Here, fp is a file pointer which points to the file that has to be closed. The function returns an
integer value which indicates whether the fclose() was successful or not. A zero is returned if
the function was successful; and a non-zero value is returned if an error occurred.
Read and Write Data from Files
C provides the following set of functions to read data from a file.
fscanf()
fgets()
fgetc()
fread()
The fscanf() is used to read data from the stream and store them
according to the parameter format into the locations pointed by the
additional arguments.
Example: Read the
contents of a file using
fscanf()
To read contents from an existing file, we
need to open that file in read mode that
means “r” mode
Algorithm to read data from a file:
1. Open the f i l e i n read mode
2. Read data from the f i l e
3. Wri t e th e data in to an outp u t
4. dev i c ea t
Repe s t eps 3 and 4 un ti l l t he
end of f i l e occurs
5. Stop procedure
fgets()
fgets() stands for file get string. The fgets() function is used to get a string
from a stream. The syntax of fgets() can be given as:
char *fgets(char *str, int size, FILE *stream);
The fgets() function reads at most one less than the number of characters
specified by size (gets size - 1 characters) from the given stream and stores
them in the string str. The fgets() terminates as soon as it encounters either
a newline character or end-of-file or any other error. However, if a newline
character is encountered it is retained. When all the characters are read
without any error, a '\0' character is appended to end the string.
Example: Read the
contents of a file using
fgets()
fgetc()
The fgetc() function returns the next character from stream, or EOF if the end
of file is reached or if there is an error. The syntax of fgetc() can be given as
int fgetc( FILE *stream );
fgetc returns the character read as an int or return EOF to indicate an error or
end of file.
fgetc() reads a single character from the current position of a file (file
associated with stream). After reading the character, the function increments
the associated file pointer (if defined) to point to the next character. However,
if the stream has already reached the end of file, the end-of-file indicator for
the stream is set.
Generally, a file contains a large amount of
data.
In a large file, it is difficult to detect the end
of file while reading.
Inorder to mark the end of a text file,
a special character EOF is stored at the
end.
Read a file character by character and simultaneously display on screen
#include<stdio.h>
void main()
{
FI LE
* f p;
char
ch ;
f p = f o p e n ( “ c l e a r. c ” , ” r ” ) ;
if(fp==NULL)
pr int( “ Unable
to
open
c l e a r. c ” ) ;
else
fread()
The fread() function is used to read data from a file. Its syntax can be given as
int fread( void *str, size_t size, size_t num, FILE *stream );
The function fread() reads num number of objects (where each object is size
bytes) and places them into the array pointed to by str. The data is read from
the given input stream.