explain the file input and file input and file output function with example program?
Answers
#include <stdio.h>
main()
{
FILE * fp1, *fp2;
char *c, str[100], *s=str;
fp1 = fopen ("inputfile" ,"r");
fp2 = fopen ("outputfile" ,"w");
if (fp1 == NULL || fp2 == NULL) {
fprintf (stderr, "error \n");
exit(1);
}
// read write each char
for ( c = fgetc (fp1); c != '\0'; ){
// do processing work on char c
fputc (fp2, c);
c = fgetc(fp1 );
}
// or, do input and output word by word...
// for (fgets (fp1,s); s ; ) {
// fputs (fp2,s);
// fgets (fp1,s);
// }
fclose (fp1) ;
fclose (fp2);
}
#include <stdio.h>
main0
FILE fp1, *fp2;
char *c, str[100], *s=str;
fp1 fopen ("inputfile","r");
fp2 fopen ("outputfile","w");
if (fp1 == NULL || fp2 = NULL)
fprintf (stderr, "error \n");
exit(1);
// read write each char
for (c fgetc (fp1); c !'0';
// do processing work on char c
fputc (fp2, c);
= fgetc(fp1);
// or, do input and output word by
word...
// for (fgets (fp1,s); s ;)
/ fputs (fp2,s);
// fgets (fp1,s)
// }
fclose (fp1);
fclose (fp2);
}