Example/* FEOF.C: This program uses feof to indicate when * it reaches the end of the file FEOF.C. It also * checks for errors with ferror. */#include <stdio.h> #include <stdlib.h>void main( void ) { int count, total = 0; char buffer[100]; FILE *stream; if( (stream = fopen( "feof.c", "r" )) == NULL ) exit( 1 ); /* Cycle until end of file reached: */ while( !feof( stream ) ) { /* Attempt to read in 10 bytes: */ count = fread( buffer, sizeof( char ), 100, stream ); if( ferror( stream ) ) { perror( "Read error" ); break; } /* Total up actual bytes read */ total += count; } printf( "Number of bytes read = %d\n", total ); fclose( stream ); } OutputNumber of bytes read = 745
CFile file("c:\\test.txt", CFile::modeRead);
DWORD dwLen = file.GetLength();
file.Read(sText.GetBuffer(dwLen), dwLen);
AfxMessageBox(sText);
* it reaches the end of the file FEOF.C. It also
* checks for errors with ferror.
*/#include <stdio.h>
#include <stdlib.h>void main( void )
{
int count, total = 0;
char buffer[100];
FILE *stream; if( (stream = fopen( "feof.c", "r" )) == NULL )
exit( 1 ); /* Cycle until end of file reached: */
while( !feof( stream ) )
{
/* Attempt to read in 10 bytes: */
count = fread( buffer, sizeof( char ), 100, stream );
if( ferror( stream ) ) {
perror( "Read error" );
break;
} /* Total up actual bytes read */
total += count;
}
printf( "Number of bytes read = %d\n", total );
fclose( stream );
}
OutputNumber of bytes read = 745
if(!(fp=fopen("C:\\Roy.txt","rb")))
return; if(fseek(fp,0l,SEEK_SET))
{
fclose(fp);
return;
} DWORD fcount;
char buf[4096];
memset(buf, 0, 4096);
while((fcount=fread(buf,sizeof(char),4096,fp)))
{
//如果你要向另一个文件写入,只要在这里写入fcount长度就可以了
MessageBox(buf);
}
fclose(fp);