Detailed function fread function prototypes

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 ().
**************************************************************************************

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:

  1. void HelpMassage()  
  2. {  
  3.     FILE *fp;  
  4.     int size = 0;  
  5.     char  * ar;  
  6.   
  7.     // open the file in binary mode  
  8.     fp = fopen("lining.txt","rb");  
  9.     if(NULL == fp)  
  10.     {  
  11.         printf("Error:Open input.c file fail!\n");  
  12.         return;  
  13.     }  
  14.   
  15.     // size of the file obtained  
  16.     fseek(fp, 0, SEEK_END);  
  17.     size = ftell(fp);  
  18.     rewind(fp);  
  19.   
  20.     // apply for a space can hold the entire file  
  21.     ar = (char*)malloc(sizeof(char)*size);  
  22.   
  23.     // read file  
  24.     fread (Ar,. 1, size, FP); // every time a read, read total size times  
  25.   
  26.     printf("%s",ar);  
  27.     fclose(fp);  
  28.     free(ar);  
  29.   
  30.     printf ( "Press any key to continue" );  
  31.     getchar();  
  32.     getchar();  
  33. }  
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();

}


Guess you like

Origin blog.csdn.net/wu694128/article/details/89709536