Unit V
Unit V
4.12 INTRODUCTION
Reading, processing and writing of data are the three essential functions of a
computer program. Most programs take some data as input and display the
processed data, often known as result. Unlike other high level languages, C
does not have any built-in input/output statements as part of its syntax.
The I/O library functions can be classified into two broad categories:
Console I/O refers to the operations that occur at the keyboard and screen
of the computer. Because input and output to the console is such a common
affair, a subsystem of the ANSI I/O file system was created to deal
exclusively with console I/O. Technically, these functions direct their
operations to the standard input (stdin) and standard output (stdout) of
the system.
Disadvantages
This works fine as long as the data is small. Real-world problems involve
large volumes of data and in such situations Console I/O functions pose two
major problems,
At these times it becomes necessary to store the data in a manner that can
be later retrieved and displayed. This medium is usually a ‘file’ on the disk.
This chapter discusses how file I/O operations can be performed.
4.13 FILES
Each file ends with an end of file (EOF) at a specified byte number,
recorded in file structure.
4.13.1 BUFFER
When the computer reads, the data move from the external device to
memory; when it writes, the data move from memory to the external
device. This data movement often uses a special work area known as
buffer.
A buffer is a temporary storage area that holds data while they are
being transferred to or from memory.
When we want to read or write files, we must use the operating system rules
when we name a file. The file name may contain two parts, a primary name
and an optional period with extension.
Example: input.txt
program.c
C has predefined structure to hold this information. The stdio.h header file
defines this file structure; its name is FILE. When we need a file in our
program, we declare it using the FILE type.
4.13.4 STREAM
Each line consists of zero or more characters and ends with the
"newline" character.
ANSI C standards specify that the system must support lines that are
at least 254 characters in length (including the new line character).
..
Stream
abcdefg
File
Figure: 4.11 data flow
1. Create a stream
2. Open a file
4. Close file
Creating a Stream
We can create a stream when we declare it. The declaration uses the FILE
type as shown below,
FILE *fp;
The FILE type is a structure that contains the information needed for reading
and writing a file, fp is a pointer to the stream.
Opening File
Once stream has been created, we can ready to associate to a file. In the
next section we will discuss in detail.
When file processing is complete, we close the file. After closing the file the
stream is no longer available.
C includes many standard functions to input data from the keyword and
output data to the monitor. For these functions, the stream that connects
our programs to the terminal is automatically created by the system and we
do not have to do anything more.
The function that prepares a file for processing is fopen. It does two things:
first, it makes the connection between the physical file and the file stream in
the program. Second, it creates a program file structure to store the
information needed to process the file as shown in Figure 4.13
#include<stdio.h> abcdefghijklmnopqrst
uvwxyz
void main () stream
….}
stream
1 2 3 ..
File
File Structure
To open a file, we need to specify the physical filename and its mode.
Syntax,
The file mode is a string that tells C compiler how we intend to use the file:
read existing file, write a new file or append to a file.
Successfully opening a file returns a pointer to (i.e., the address of) a file
structure. The actual contents of the FILE are hidden from our view. All we
need to know is that we can store the address of the file structure and use it
to read or write the file.
Once the files are open, they stay open until you close them or end the
program (which will close all files.)
File Mode
When we open a file, we explicitly define its mode. The mode shows how we
will use the file: for reading, for writing, or for appending.
The read mode (r) opens an existing file for reading. When a file is opened in
this mode, the file marker is positioned at the beginning of the file (first
character). The file marker is a logical element in the file structure that
keeps track of our current position in the file.
The file must already exist: if it does not, NULL is returned as an error. Files
opened for reading are shown in Figure 4.14. If we ry t to write a file opened
in read mode, we get an error message.
The write mode (w) opens for writing. If the file doesn’t exist, it is created.
IF it is already exists, it is opened and all its data are erased; the file marker
is positioned at the beginning of the file (first character) It is an error to try
to read from a file opened in write mode. A file opened for writing is shown
in figure 4.14.
The append mode (a) also opens an existing for writing. Instead of creating
a new file, however, the writing starts after the last character; that is new
data is added, or appended, at the end of the file.
K.SRINIVASA RAO PNO: 61
IF the file doesn’t exist, it is created and opened. In this case, the writing
will start at the beginning of the file; File opened in append mode are shown
in Figure 4.14.
In this mode file is opened for both reading and writing the data. If a file
does not exist then NULL, is returned.
In this mode file is opened for both writing and reading the data. If a file
already exists its contents erased. If a file does not exist then new file
created.
In this mode file is opened for reading the data as well as data can be added
at the end.
The above figure shows the file marker position in different modes.
K.SRINIVASA RAO PNO: 62
File Close (fclose)
fclose (file_pointer);
Once a file is closed, its file pointer can be reused for another file.
We have already familiar with two formatting functions scanf and printf.
These two functions can be used only with the keyboard and monitor. The C
library defines two more general functions, fscanf and fprintf, that can be
used with any txt stream.
This function provides for formatted output to the screen. The syntax
is:
The “format string” includes a listing of the data types of the variables
to be output and, optionally, some text and control character(s).
Example:
This function provides for formatted input from the keyboard. The
syntax is:
float a; int b;
The first argument is the stream pointer, is the pointer to the streams
that has been declared and associated with a text file. Remaining is
same as scanf function arguments.
int a, b;
FILE *fptr1;
The fscanf function would read values from the file "pointed" to by
fptr1 and assign those values to a and b.
The only difference between scanf and fscanf is that scanf reads data
from the stdin (input stream) and fscanf reads input from a user
specified stream(stdin or file).
End of File
The end-of-file indicator informs the program when there are no more data
(no more bytes) to be processed. There are a number of ways to test for the
The format string contains output specifications for the items in the
list.
int a = 5, b = 20;
FILE *fptr2;
fprintf function works like printf except that it specifies the file in which
the data will be displayed. The file can be standard output (stdout) or
standard error (stderr) also.
Example,
// OUTPUT
Character input functions read one character at a time from a text stream.
Character output functions write one character at the time to a text stream.
This function is to read exactly one character from the keyboard; it reads the
next character from the standard input stream.
ch =getchar ();
Its return value is integer. Up on successful reading returns the ASCII value
of character. If there is an error returns EOF.
This function provides for printing exactly one character to the Monitor.
Its return value is integer. Up on successful writing returns the ASCII value
of character. If there is an error returns EOF.
When used with a terminal device, the streams are declared and
opened by the system, the standard input stream (stdin) for the
keyword and standard output stream (stdout) for the monitor.
The getc functions read the next character from the file stream, which can
be a used-defined stream or stdin, and converts it in to an integer. This
function has one argument which is the file pointer declared as FILE.
If the read detects an end of file, the function returns EOF, EOF is also
returned if any error occurs. The functionality of getc/fgetc same.
getc (fp);
fgetc (fp);
Example,
char ch;
The putc functions write a character to the file stream specified which can be
a user-defined stream, stdout, or stderr. The functionality of putc/ fputc
same.
For fputc, the function takes two arguments. The first parameter is the
character to be written and the second parameter is the file, The second
parameter is the file pointer declared as FILE.
if the character is successfully written the functions returns it If any error
occurs, it returns EOF.
Syntax,
char ch;
The gets and fgets function take a line (terminated by a new line) from the
input stream and make a null-terminated string out of it. These are
sometimes called line-to-string input function.
gets ()
The newline (\n) indicates the end of the string, it is replaced by null
(\0) character in the memory by the function gets () as shown in
Figure 4.15.
gets function takes only one parameter, the name of the string, its
syntax is as follows,
gets (string_name);
fgets ()
It reads characters from the specified file until a new line character
has been read or until n-1 characters has been read, whichever occurs
first. The function automatically places null character at the end as
shown in Figure 4.15.
First argument is name of the string in which the read data from the
file is to be placed. Second argument is the length of the string to be
read and last argument is the file pointer.
fgets also reads data from terminal. Below example illustrates this,
puts function takes only one parameter, the name of the string, its
syntax is as follows,
puts (string_name);
fputs ()
This function scans a string through the data were coming from a file. Just
like fscanf, it requires a format string to provide the formatting parameters
for the data.
Syntax,
The first argument is the string, which contains the data to be scanned. The
second is the control string consisting of format Specifiers. Variables, it is a
list of variables in to which formatted data is copied.
Its return value is integer. If the sscanf () is successful it returns how many
variables formatted. The error is specified with EOF.
Example,
The string print function is sprintf, follows the rules of fprintf. Rather than
sending the data to a file. however, it simply “writes” them to a string. When
all data have been formatted to the string, a terminating null character is
added to make the result a valid string. If an error is detected, sprintf
returns any negative value or EOF. If the formatting is successful, it returns
number of characters formatted.
Syntax,
The first argument is the string, which contains the data to written. The
second is the control string consisting of format Specifiers. Variables, it is a
list of variables in to which formatted data is copied.
Example,
Text File
A text file is the one where data is stored as a stream of characters that can
be processed sequentially. In the text format, data are organized into lines
terminated by newline character. The text files are in human readable form
and they can be created and read using any text editor. Since text files
process only characters, they can read or write only one character at a time.
A new line may be converted to carriage return and line feed character.
Figure 4.19 shows the data transfer in text file.
For example, the data 2345 requires 4 bytes with each character occupying
exactly one byte. A text file cannot contain integers, floating – point
numbers etc. converted to their character – equivalent formats.
Binary File
A binary file is the one where data is stored on the disk in the same way as
it is represented in the computer memory. The binary files are not in human
readable form and they can be created and read only by specific programs
written for them. The binary data stored in the file cannot be read using any
of the text editors.
The differences between text file and binary file are as shown in below Table.
The following Figure 4.21 illustrates the storage d ifference between text file
and binary file.
2. Data is stored as lines of 2. Data is stored on the disk in the same way as
characters with each line it is represented in the computer memory.
terminated by / n which may
be translated into carriage
return + line feed.
4. Data can be read using any 4. Data can be read only by specific programs
of the text editors. written for them.
7. The data 2345 requires 4 7. The data 2345 requires 2 bytes and stored as
bytes with each character 0x0929. The data 2345 is requiring 2 bytes.
requiring one byte. So, it is
stored as 0x 32, 0x 33, and 0 x
34, 0 x 35 (ASCII values) thus
requiring 4 bytes.
The basic operation is unchanged for binary files-only the mode changes.
Different modes of operation of binary file are illustrated in below Table.
Mode Meaning
r+b This mode opens a pre-existing binary file in read and write
mode.
Just like text files, binary files must be closed when they are not needed
anymore using fclose ().
C language uses the block input and output functions to read and write data
to binary files. As we know that data are stored in memory in the form of 0’s
and 1’s. When we read and write the binary files, the data are transferred
just as they are found in memory and hence there are no format
conversions. The block read function is file read(fread). The block write
function is file write (fwrite).
This function reads a specified number of bytes from a binary file and places
them into memory at the specified location. This function declaration is as
follows,
int fread (void *pInArea, int elementsize, int count, FILE *sp);
The first parameter, pInArea, is a pointer to the input area in memory. The
data read from the file should be stored in memory. For this purpose, it is
required to allocate the sufficient memory and address of the first byte is
stored in pInArea. We say that pInArea now points to buffer.
This function returns the number of items read. If no items have been read
or when error has occurred or EOF encountered, the function returns 0.
File read expects a pointer to the input area, which is usually a structure.
This is because binary files are most often used to store structures.
Figure 4.22 is an example of a file read that reads data into an array of
integers. When fread is called, it transfers the next three integers from the
file to the array, inArea.
This function writes specified number of items to a binary file. This function
declaration is as follows,
int fwrite (void *pInArea, int elementsize, int count, FILE *sp)
The parameters for file write correspond exactly to the parameters for the
file read function. Figure 4.23 shows the write ope ration that parallels the
read in Figure 4.22.
File positioning functions have two uses. First, for randomly processing data
in disk files, we need to position the file to read the desired data. Second,
we can use the positioning functions to change a file’s state. Thus, we can
change the state of the file pointer using one of the positioning functions.
tell location
rewind file
file seek
The ftell function reports the current position of the file marker in the
file, relative to the beginning of the file.
ftell () takes a file pointer and returns a number of type long integer,
that corresponds to the current position.
Here the return type is long integer because many files have more
than 32,767 bytes. The operation ftell is graphically shown in Figure
4.24.
n=ftell (fp);
The above Figure 4.24 illustrates the working of ftell, it returns 16, and
the current offset value.
The rewind function simply sets the file position indicator to the
beginning of the file as shown in Figure 4.25.
This function helps us in reading a file more than once, without having
to close and open the file.
Its common use is to change a work from a write state to a read state.
However, that to read and write a file with only one open, we must
open it in update mode w+.
Example:
rewind (fp);
n=ftell (fp);
Would assign 0 to n because the file position has been set to the start of
the file by rewind.
Value Meaning
0 beginning of file.
1 Current position.
2 End of file.
Statement Meaning
Device overflow.
We have two status-inquiry library functions, feof () and ferror () that can
help us detect I/O errors in the files.
The feof function can be used to test for an end of file condition.
It takes a FILE pointer as its only argument.If all data have been read,
the function returns a nonzero (positive value) and returns zero (false)
otherwise.
K.SRINIVASA RAO PNO: 96
It takes of the following form,
n=feof (fp);
If fp is a pointer to file that has just been opened for reading, then the
statement
if (feof (fp))
Would display the message “End of data” on reaching the end of file
condition.
The ferror function reports the status of the file indicated. Error can be
created for many reasons, such as trying to read a file in the write
state.
n=ferror (fp);
It also takes a FILE pointer as its argument and returns a non zero
integer if an error has been detected up to that point, during
processing. Otherwise, it returns zero.
The statement
if(ferror(fp)!=0)
However, that testing for an error not reset the error condition. Once a file
enters the error state. It can only return to a read or write state by calling
clear error function: clearerr.
The first parameter argc stands for the argument count, which is of integer
data type. Its value is the number of arguments in the command line that
was used to execute the program.
The second parameter argv stands for the argument vector. It is an array of
pointers to the character data type. The program name is the first parameter
on the command line, which is argv [0].
#include<stdio.h>
int j;
return 0;
}
K.SRINIVASA RAO PNO: 98
OUTPUT:
argument 1 is one
argument 2 is two
argument 3 is three