Home > Programming > Functions using the "C" programming language > example22.c |
/*
In this example we demonstrate how to use file pointers as function
parameters
*/
#include <stdio.h>
#include <stdlib.h>
/*
This program make a copy of an ascii file.
*/
/*
Function prototype
*/
void filecopy( FILE *, FILE * );
void main(void)
{
char isn[80],osn[80]; /* Input and output filenames */
FILE *fi,*fo; /* Input and output files */
printf("Give the input file : ");
scanf("%s",isn);
printf("Give the output file : ");
scanf("%s",osn);
if ((fi=fopen(isn,"r")) == NULL)
/* Open input file. */
{
printf("Can not open input file
%s.\n",isn);
exit( 1 );
}
if ((fo=fopen(osn,"w")) == NULL)
/* Open output file. */
{
printf("Can not create output file
%s.\n",osn);
exit( 1 );
}
filecopy( fi, fo );
fclose(fi); /* Close input file. */
fclose(fo); /* Close output file. */
}
/*
Here is the source code of function filecopy.
Function filecopy takes two file pointers parameters and copy the
contents of
the first file parameter to the second.
*/
void filecopy( FILE *ifp, FILE *ofp )
{
int c;
/*
Ascii copy using getc and putc.
*/
while ( ( c = getc( ifp ) ) != EOF
)
putc( c, ofp );
/*
Finally we write the EOF character to the output file
*/
putc( EOF, ofp );
}
© 2004 Jim Valavanis