Professional Documents
Culture Documents
C FILE Programs
C FILE Programs
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 ) ;
}
---------------------------
fp = fopen ( "PR1.C", "r" ) ;
if ( fp == NULL )
{
puts ( "cannot open file" ) ;
exit( ) ;
}
---------------------------
/* Count chars, spaces, tabs and newlines in a file */
# include "stdio.h"
main( )
{
FILE *fp ;
char ch ;
int nol = 0, not = 0, nob = 0, noc = 0 ;
fp = fopen ( "PR1.C", "r" ) ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
noc++ ;
if ( ch == ' ' )
nob++ ;
if ( ch == '\n' )
nol++ ;
if ( ch == '\t' )
not++ ;
}
fclose ( fp ) ;
printf ( "\nNumber of characters = %d", noc ) ;
printf ( "\nNumber of blanks = %d", nob ) ;
printf ( "\nNumber of tabs = %d", not ) ;
printf ( "\nNumber of lines = %d", nol ) ;
}
---------------------------
// copy content of a file into another file
fs = fopen ( "pr1.c", "r" ) ; //source file to copy
ft = fopen ( "pr2.c", "w" ) ; //target file
if ( ft == NULL )
{
puts ( "Cannot open target file" ) ;
fclose ( fs ) ;
exit( ) ;
}
while ( 1 )
{
ch = fgetc ( fs ) ;
if ( ch == EOF )
break ;
else
fputc ( ch, ft ) ;
"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.
"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.
---------------------------
//write string to a writable file
char s[80] ;
printf ( "\nEnter a few lines of text:\n" ) ;
while ( strlen ( gets ( s ) ) > 0 )
{
fputs ( s, fp ) ;
fputs ( "\n", fp ) ;
}
//string is terminated by hitting enter.
---------------------------
char s[80] ;
while ( fgets ( s, 79, fp ) != NULL )
printf ( "%s" , s ) ;