2. INTRODUCTION
๏ต Until Now We Have Been Using The Function Such As Scanf
And Printf To Read And Write Data. These Are Console
Oriented I/O Functions, Which Always Use The Terminal As
The Target.
๏ต The Console Oriented I/O Pose Two Major Problem:
1. It Becomes Complex And Time Consuming To Handle Large
Volume Of Data Through Terminal.
2. The Entire Data Is Lost When Either The Program Is
Terminated.
3. ๏ตA File Is A Place On The Disk Where A Group
Of Related Data Is Stored. Like Other
Language C Supports A Number Of Function
That Has The Ability To Perform Basic File
Operation, Which Include:
1. Naming A File
2. Opening A File
3. Reading Data
4. Writing Data
5. Closing A File
4. FUNCTION NAME OPERATION
๏ fopen() ๏ผ Creates a new file for use.
๏ fclose() ๏ผ Close a file which is open for use.
๏ getc() ๏ผ Reads a character from file.
๏ putc() ๏ผ Writes a character to a file.
๏ fprint() ๏ผ Writes a set of data values to the file.
๏ fscanf() ๏ผ Reads a set of data value from a file.
๏ getw() ๏ผ Read an integer from the file.
HIGH LEVEL I/O
FUNCTION
5. DEFINING AND OPENING A FILE
๏If we want to store data in a file in the secondary memory,
we must specify certain things about the file. They include:
1) FILENAME
2) DATA STRUCTURE
3) PURPOSE
๏ตDATA STRUCTURE OF A FILE IS DEFINED AS FILE. FOLLOWING is
the general format for declaring and opening a file.
๏ต FILE *fp;
๏ต fp = fopen(โfilenameโ , โmodeโ);
6. CLOSING A FILE
๏ตA file must be closed as soon as operation on it have
been completed. Another instance where we have to
close a file is when we want to reopen it.
๏ตThe I/O library supports a function to do this for us. It
takes the following forms:
fclose(file_ pointer);
7. GETC AND PUTC FUNCTION
๏ต The simplest file I/O functions are getc and putc. These are
the getchar and putchar functions and handle one character
at a time. The statement:
putc(c , fp1);
๏ต Similarly, getc is used to read the character from a file that
has been opened in read mode. The statement:
c= getc(fp2) ;
8. GETW AND PUTW FUNCTION
๏ตThe getw and putw are integer-oriented functions.
They are similar to the getc and putc functions
and are used to read and write integer values. The
general form are:
putw(integer ,fp);
getw(fp);
9. THE fprintf and fscanf FUNCTION
๏ The function fprintf and fscanf perform I/O operation that
are identical to the familiar printf and scanf functions,
except of course that they work on files. The general form:
fprintf(fp , โcontrol stringsโ ,list);
๏ The list may include constant, variable and strings. The
general format of fscanf is:
fprintf(fp , โcontrol stringsโ ,list);
10. ERROR HANDLING DURING I/O OPERATOR
๏ตIt is possible an error may occur during I/O
operation on a file. They are:
1) Trying to read beyond the end- of- line mark.
2) Device overflow
3) Trying to use a file that has not been opened.
4) Trying to perform an operation on a file, when
the file is opened for another type of operation.
5) Opening a file with an invalid filename.
6) Attempting to write to a write-protected file.
11. RANDOM ACESS TO FILE
๏ตIt can be achieved with the help of the functions fseek,
ftell, and rewind available in the I/O library.
๏ตFtell takes a file pointer and return a number of type
long, that corresponds to the current position.
n = ftell(fp);
๏ตRewind takes a file pointer and resets the position to
the start of the file.
rewind(fp);
n = ftell(fp);
๏ตfseek function is used to move the file position to a
desired location within the file.
fseek(file_ ptr , offset, position);
12. COMMAND LINE ARGUMENT
๏ต Itโs a parameter supplied to a program when the program is
invoked. For example , if we want to copy the contents of a file
X_FILE to another named Y_FILE, then:
C > PROGRAM X_ FILE Y_ FILE
๏ต Where PROGRAM is the filename where the executable code of
the program is stored.