Reprinted _fread function fread function Detailed Detailed

Detailed function fread

Prototype:
fread size_t (void * Buffer, size_t size, size_t COUNT, FILE * Stream) 
  Buffer is a pointer to the data stored in the memory read (can be an array, or a new open space, buffer is an index)   
 size every time number of bytes read  
 
  count is the number of readings  
  strean read pointer to the file  
  , for example, 100 bytes is read from the file is available in the following statement fp  
  fread (Buffer, 100,1, fp)  
  fread (Buffer, 50,2, fp)  
  fread ( Buffer, 1,100, fp)   
**************************** ******************************************    
to read binary stream is not seeking the length and size with strlen () or the sizeof () a.
**************************************************************************************

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 ;

    // binary mode to open the file 
    fp = fopen ( " lining.txt " , " rb " );
     IF (NULL == fp)
    {
        printf("Error:Open input.c file fail!\n");
        return;
    }

    // obtain the size of the file 
    fseek (fp, 0 , SEEK_END);
    size = ftell(fp);
    rewind(fp);

    // Application can hold an entire file space 
    Ar = ( char *) the malloc ( the sizeof ( char ) * size);

    // read file 
    fread (Ar, 1 , size, fp); // each read a total read size times 

    printf ( " % S " , Ar);
    fclose(fp);
    free(ar);

    printf ( " Press any key to continue " );
    getchar();
    getchar();
}

 

Original Address: https://www.cnblogs.com/melons/p/5791874.html

Guess you like

Origin www.cnblogs.com/haoyufang/p/11787150.html