Prototype:
size_t fread( void *buffer, size_t size, size_t count, FILE *stream )
buffer pointer data is stored in the memory is read (can be an array, or may be newly opened space, that is, an index buffer) size is the number of bytes to read per
count is the number of reads
strean pointer file is to be read
, for example, to read 100 bytes from a file available in the following statement in fp
fread(buffer,100,1,fp)
fread(buffer,50,2,fp)
fread (Buffer, 1,100, fp)
****************************************** ********************************************
to read binary stream can not find its length and size with strlen () or sizeof ().
buffer pointer data is stored in the memory is read (can be an array, or may be newly opened space, that is, an index buffer) size is the number of bytes to read per
count is the number of reads
strean pointer file is to be read
, for example, to read 100 bytes from a file available in the following statement in fp
fread(buffer,100,1,fp)
fread(buffer,50,2,fp)
fread (Buffer, 1,100, fp)
****************************************** ********************************************
to read binary stream can not find its length and size with strlen () or sizeof ().
**************************************************************************************
fread can read binary files, sometimes to read a character mode file can not read the entire document, but can be binary mode.
This is because the character mode with a specific end-labeled, as long as the encounter when reading the mark is automatically ended
Function fread () reads [ NUM ] object (the size of each object size (size) specified number of bytes), and to replace them by the buffer specified (buffer) array of data from the input stream given by the return value is a function of the amount of content to read ...
Use feof () or ferror () to determine what errors occurred in the end.
On a piece of code:
- void HelpMassage()
- {
- FILE *fp;
- int size = 0;
- char * ar;
- // open the file in binary mode
- fp = fopen("lining.txt","rb");
- if(NULL == fp)
- {
- printf("Error:Open input.c file fail!\n");
- return;
- }
- // size of the file obtained
- fseek(fp, 0, SEEK_END);
- size = ftell(fp);
- rewind(fp);
- // apply for a space can hold the entire file
- ar = (char*)malloc(sizeof(char)*size);
- // read file
- fread (Ar,. 1, size, FP); // every time a read, read total size times
- printf("%s",ar);
- fclose(fp);
- free(ar);
- printf ( "Press any key to continue" );
- getchar();
- getchar();
- }
void HelpMassage() { FILE *fp; int size = 0; char *ar ;
//二进制方式打开文件 fp = fopen("lining.txt","rb"); if(NULL == fp) { printf("Error:Open input.c file fail!\n"); return; } //求得文件的大小 fseek(fp, 0, SEEK_END); size = ftell(fp); rewind(fp); //申请一块能装下整个文件的空间 ar = (char*)malloc(sizeof(char)*size); //读文件 fread(ar,1,size,fp);//每次读一个,共读size次 printf("%s",ar); fclose(fp); free(ar); printf("按任意键继续"); getchar(); getchar();
}