C LANGUAGE BOOK |
The FILE structure has been defined in the header file “stdio.h”
FILE *fp ;
fopen( ) returns the address of this structure, which we have
collected in the structure pointer called fp.
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.
rb:-open a binary file for reading.
wb:-create a binary file for writing.
ab:-append to a binary file
r+b:-open a binary file for read/write.
w+b:-create a binary file for read/write.
a+b:-append or create a binary file for read/write.
fs = fopen ( argv[1], "r" ) ;
it open file in reading mode.r denotes mode.
ft = fopen ( argv[2], "w" ) ;
it open file in writing mode.w denotes mode.
EOF denotes end of file.The EOF macro has been defined in the
file “stdio.h”.
fgetc( ) which reads characters from a file. The file signified by file pointer.
fputc( ) which writes characters to a file. The file signified by file pointer.
Reading from a File
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 ) ;
Writing to a File
To write the content to a file. there exists a function
called fputc( ). fputc ( ch, ft ) ;
ch is character.ft is file pointer in our program.
writes strings to a file
writes strings to a file using the function fputs( ).
fputs ( s, ft ) ;
s is string.ft is file pointer.
fputs( ) function then writes the contents of the array to the disk. Since
fputs( ) does not automatically add a newline character to the end
of the string, we must do this explicitly to make it easier to read
the string back from the file.
fputs ( "\n", ft ) ;
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 ) ;
if you save program as filecopy.c.then
To compile the program type at command
prompt, in the form:
filecopy PR1.C PR2.C
where, PR1.C is the source filename and PR2.C is the target
filename.
/*
WAP TO COPY ONE FILE TO ANOTHER.
*/
#include "stdio.h"
main ( int argc, char *argv[ ] )
{
FILE *fs, *ft ;
char ch ;
if ( argc != 3 )
{
puts ( "Improper number of arguments" ) ;
exit( ) ;
}
fs = fopen ( argv[1], "r" ) ;
if ( fs == NULL )
{
puts ( "Cannot open source file" ) ;
exit( ) ;
}
ft = fopen ( argv[2], "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 ) ;
}
fscanf( )
fscanf( ) library functions to read data from file.
fscanf ( fp, "%s %d %f", e.name, &e.age, &e.bs )
fp is file pointer.
fprintf( ):-
fprintf( ), which writes the values in the structure variable to the file.
This function is similar to printf( ), except that a FILE pointer is
included as the first argument.
fprintf ( fp,"\n%s %d %f", e.name, e.age, e.bs ) ;
fp is file pointer.
fflush( ).:-
It is designed to remove or ‘flush out’ any data remaining in the buffer.
The argument to fflush( ) must be the buffer which we want to flush out.
Here we have used ‘stdin’, which means buffer related with standard input
device—keyboard.
/* Read records from a file using structure */
#include "stdio.h"
main( )
{
FILE *fp ;
struct emp
{
char name[40] ;
int age ;
float bs ;
} ;
struct emp e ;
fp = fopen ( "EMPLOYEE.DAT", "r" ) ;
if ( fp == NULL )
{
puts ( "Cannot open file" ) ;
exit( ) ;
}
while ( fscanf ( fp, "%s %d %f", e.name, &e.age, &e.bs ) != EOF )
printf ( "\n%s %d %f", e.name, e.age, e.bs ) ;
fclose ( fp ) ;
}
And here is the output of the program...
x 34 1250.500000
xx 21 1300.500000
xxx 34 1400.500000
C LANGUAGE BOOK |
No comments:
Post a Comment