What is a File
A file is a collection of data or we can say it is used to store Records.
File Operations :
There are different operations that can be carried out on a file.
These are:
(a)Creation of a new file
(b)Opening an existing file
(c)Reading from a file
(d)Writing to a file
(e)Moving to a specific location in a file (seeking)
(f)Closing a file
/* Display contents of a file on screen. */
#include <stdio.h>
#include <conio.h>
main( )
{
FILE *fp ;
char ch ;
fp = fopen ( "PR1.C", "r" ) ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
printf ( "%c", ch ) ;
}
fclose ( fp ) ;
}
On execution of this program it displays the contents of the file ‘PR1.C’ on the screen.
Let us now understand how it does the same.
Opening a File
Before we can read (or write) information from (to) a file on a disk we must open the file.
To open the file we have called the function fopen( ).
It would open a file “PR1.C” in ‘read’ mode, which tells the C compiler that we would be reading the contents of the file.
Note that “r” is a string and not a character; hence the double quotes and not single quotes.
In fact fopen( ) performs three important tasks when you open the file in “r” mode:
(a)Firstly it searches on the disk the file to be opened.
(b)Then it loads the file from the disk into a place in memory
called buffer.
(c)It sets up a character pointer that points to the first character
of the buffer.
Reading from a File :
Once the file has been opened for reading using fopen( ), as we have seen, the file’s contents are brought into buffer (partly or wholly) and a pointer is set up that points to
the first character in the buffer. This pointer is one of the elements of the structure to which fp is pointing.
To read the file’s contents from memory there exists a function called fgetc( ).
This has been used in our program as,
ch = fgetc ( fp ) ;
fgetc( ) reads the character from the current pointer position, advances the pointer position so that it now points to the next character, and returns the character that is read, which we collected in the variable ch. Note that once the file has been opened, we no longer refer to the file by its name, but through the file pointer fp.
We have used the function fgetc( ) within an indefinite while loop.
There has to be a way to break out of this while.
When shall we break out... the moment we reach the end of file.
But what is end of file? A special character, whose ASCII value is 26, signifies end of file. This character is inserted beyond the last character in the file, when it is created.
While reading from the file, when fgetc( ) encounters this special character, instead of returning the character that it has read, it returns the macro EOF. The EOF macro has
been defined in the file “stdio.h”. In place of the function fgetc( ) we could have as well used the macro getc( ) with the same effect.
Closing the File :
When we have finished reading from the file, we need to close it.
This is done using the function fclose( ) through the statement, fclose ( fp ) ;
Once we close the file we can no longer read from it using getc( ) unless we reopen the file.
Note that to close the file we don’t use the filename but the file pointer fp. On closing the file the buffer associated with the file is removed from memory.
When we close this file using fclose( ) three operations would be performed:
(a)The characters in the buffer would be written to the file on the disk.
(b)At the end of file a character with ASCII value 26 would get
written.
(c)The buffer would be eliminated from memory.
A File-copy Program
We have already used the function fgetc( ) which reads characters from a file.
Its counterpart is a function called fputc( ) which writes characters to a file.
As a practical use of these character I/O functions we can copy the contents of one file into another, as demonstrated in the following program. This program takes the contents of a file and copies them into another file, character by character.
#include "stdio.h"
main( )
{
FILE *fs, *ft ;
char ch ;
fs = fopen ( "pr1.c", "r" ) ;
if ( fs == NULL )
{
puts ( "Cannot open source file" ) ;
exit( ) ;
}
}
ft = fopen ( "pr2.c", "w" ) ;
if ( ft == NULL )
{
puts ( "Cannot open target file" ) ;
fclose ( fs ) ;
exit( ) ;
}
while ( 1 )
{
ch = fgetc ( fs ) ;
if ( ch == EOF )
break ;
else
fputc ( ch, ft ) ;
}
fclose ( fs ) ;
fclose ( ft ) ;
}
Writing to a File
The fputc( ) function is similar to the putch( ) function, in the sense that both output characters. However, putch( ) function always writes to the VDU, whereas, fputc( ) writes to the file.
Which file? The file signified by ft. The writing process continues till all characters from the source file have been written to the target file, following which the while loop terminates.
File Opening Modes :
In our first program on disk I/O we have opened the file in read (“r”) mode. However, “r” is but one of the several modes in which we can open a file. Following is a list of all possible modes in
which a file can be opened. The tasks performed by fopen( ) when a file is opened in each of these modes are also mentioned.
"r" :
Searches file. If the file is opened successfully fopen( )
loads it into memory and sets up a pointer which points to
the first character in it. If the file cannot be opened fopen( )
returns NULL.
Operations possible – reading from the file.
"w" :
Searches file. If the file exists, its contents are overwritten.
If the file doesn’t exist, a new file is created. Returns
NULL, if unable to open file.
Operations possible – writing to the file.
"a" :
Searches file. If the file is opened successfully fopen( )
loads it into memory and sets up a pointer that points to the
last character in it. If the file doesn’t exist, a new file is
created. Returns NULL, if unable to open file.
Operations possible - adding new contents at the end of file.
"r+" :
Searches file. If is opened successfully fopen( ) loads it into
memory and sets up a pointer which points to the first
character in it. Returns NULL, if unable to open the file.
Operations possible - reading existing contents, writing new
contents, modifying existing contents of the file.
"w+" :
Searches file. If the file exists, its contents are overwritten.
If the file doesn’t exist a new file is created. Returns NULL,
if unable to open file.
Operations possible - writing new contents, reading them
back and modifying existing contents of the file.
"a+" :
Searches file. If the file is opened successfully fopen( )
loads it into memory and sets up a pointer which points to
the first character in it. If the file doesn’t exist, a new file is
created. Returns NULL, if unable to open file.
Operations possible - reading existing contents, appending
new contents to end of file. Cannot modify existing
contents.
Prev NEXT
0 comments:
Post a Comment