/* EOF.C: This program reads data from a file * ten bytes at a time until the end of the * file is reached or an error is encountered. */ #include <io.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> void main( void ) { int fh, count, total = 0; char buf[10]; if( (fh = _open( "eof.c", _O_RDONLY )) == - 1 ) { perror( "Open failed"); exit( 1 ); } /* Cycle until end of file reached: */ while( !_eof( fh ) ) { /* Attempt to read in 10 bytes: */ if( (count = _read( fh, buf, 10 )) == -1 ) { perror( "Read error" ); break; } /* Total actual bytes read */ total += count; } printf( "Number of bytes read = %d\n", total ); _close( fh ); } OutputNumber of bytes read = 754
sorry, 试过了,这种办法不行.
fp = fopen() count = fread( fp ..) if ( count == 0 ) // end of file
* ten bytes at a time until the end of the
* file is reached or an error is encountered.
*/
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
int fh, count, total = 0;
char buf[10];
if( (fh = _open( "eof.c", _O_RDONLY )) == - 1 )
{
perror( "Open failed");
exit( 1 );
}
/* Cycle until end of file reached: */
while( !_eof( fh ) )
{
/* Attempt to read in 10 bytes: */
if( (count = _read( fh, buf, 10 )) == -1 )
{
perror( "Read error" );
break;
}
/* Total actual bytes read */
total += count;
}
printf( "Number of bytes read = %d\n", total );
_close( fh );
}
OutputNumber of bytes read = 754
count = fread( fp ..)
if ( count == 0 ) // end of file
结束
getc返回的是一个整形的。