/* Kenneth L Moore CIT 145 22/01/02 07:13 file fileio.c This program demonstrates file input/output */ #include #include #define FILE_IN "fileio.c" /* file to read from */ #define FILE_OUT "fileio.tmp" /* file to write to */ int main() { FILE *infil, *outfil; /* pointer variables of type FILE needed */ int c; int mm; char str[41]; infil = fopen( FILE_IN, "r" ); /* open for input processing */ if ( infil == NULL ) { fprintf( stderr, "Unable to open input file %s\n", FILE_IN ); exit( 8 ); } if ( (outfil = fopen( FILE_OUT, "w" )) == NULL ) /* open for output */ { fprintf( stderr, "Unable to open output file %s\n", FILE_OUT ); exit( 8 ); } while( ( c = fgetc( infil ) ) != EOF ) /* read to end, char-by-char */ { if ( c == ' ' ) /* change spaces to underscores */ { c = '_'; } fputc( c, outfil ); /* write character to output file */ } fclose( infil ); fclose( outfil ); infil = fopen( FILE_IN, "r" ); /* read again, word-by-word */ if ( infil == NULL ) { fprintf( stderr, "Unable to open input file %s\n", FILE_IN ); exit( 8 ); } while( fscanf( infil, "%40s", str ) != EOF ) { fprintf( stdout, "<%s>\n", str ); } fclose( infil ); scanf("%d",&mm); return( 0 ); }