Many
real-life problems involve large volumes of data and in such situations; the
console oriented I/O operations (scanf, printf) pose two major problems:
(a) It becomes
cumbersome and time consuming to handle large volumes of data through
terminals.
(b) The
entire data is lost when either the program is terminated or the computer is
turned off.
It
is necessary to have a more flexible approach where data can be stored on the
disks and read whenever necessary, without destroying the data. This method
employs the concept of files.
Def:
A file is a place on the disk where
a group of related data is stored. C supports a number of functions that have
the ability to perform basic file operations, which include:
·
Naming a file
·
Opening a file
·
Reading data from a file
·
Writing data to a file
·
Closing a file
ü Defining and opening a file
o
If we want to store data in a file we must
specify certain things about the file to the operating system i.e. filename,
data structure, purpose.
o
Filename is a string of
characters that make up a valid filename. It may contain two parts, a primary
name and an optional period with extension.
o
Data structure of a file is
defined as FILE in the library of standard I/O function definitions.
o
When we open a file, we must specify what
we want to do with the file i.e. what is the purpose of the file.
General
Format:
FILE
*fp;
fp = fopen (“filename”, “mode”);
o
The first statement declares the variable fp as a “pointer to the data type
FILE”.
o
The second statement opens the file named
‘filename’ and assigns an identifier to the FILE type pointer fp.
NOTE: This pointer (i.e. fp), contains all the
information about the file is subsequently used as a communication link between
the system and the program.
o
The second statement also specifies the
purpose of opening this file. The mode does this job. Mode can be one of the
following:
§ r :
open the file for reading only
§ w : open
the file for writing only
§ a :
open the file for appending (or adding) data to it
ü
Closing
a file
o
A file must be closed as soon as all
operations on it have been completed. This ensures that all outstanding
information associated with the file is flushed out from the buffers and all
links to the file are broken.
o
Reasons for closing a file:
§ Prevents
any accidental misuse of the file.
§ There
is a limit to the number of files that can be kept open simultaneously.
§ Want to
reopen the same file in a different mode.
General format:
fclose(file_pointer);
This
would close the file associated with the FILE pointer file_pointer.
Sample Code:
…………
FILE
*p1, *q2;
p1 = fopen(“INPUT”, “w”);
q2 = fopen(“OUTPUT”, “r”);
………….
………….
fclose (p1);
flcose (q2);
…………..
ü Input/Output operations on file
(i)
getc( ) and putc( ) Functions
Assume
that a file is opened with mode w and file ponter fp1. Then, the statement,
putc (v, fp1);
writes
the character contained in the character variable v to the file associated with the FILE pointer fp1.
Similarly,
getc( ) is used to read a chacater from a file that has been opened in read
mode.
v = getc(fp2);
NOTE: The getc( ) will return an end-of-file
marker EOF, when end of the file has been reached.
NOTE: The end of the data is indicated by
entering an EOF character, which is control-Z in the reference system.
(ii)
getw( ) and putw( ) Functions
The
getw( ) and putw( ) are integer oriented functions and are thus sued to read
and write integer values.
General form:
putw( integer, fp);
getw( fp);
(iii)
fprintf( ) and fscanf( ) Functions
The
functions fprintf( ) and fscanf( ) perform I/O operations that are identical to
the familiar printf( ) and scanf( ) functions, except that they work on files.
General form:
fprintf( fp, “control string”,
list);
where, fp is a file pointer associated with a
file that has been opened for writing.
control string contains output specifications for the items in the
list.
list may include variables, constants and strings.
e.g. fprintf( f1, “%s%d%f”, name,age,7.5);
The
general format of fscanf( ) is:
fscanf( fp , “Control string”,
list);
This
statement would cause the reading of the items in the list from the file
specified by fp, according to the
specifications contained in the control string.
e.g. fscanf( f2, “%s%d”, &item,
&quantity);
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.