The function fread () in C

Function size_t fread (void * ptr, size_t size, size_t nmemb, FILE * stream) in standard C Library reads data from the given stream into the pointed array, by ptr .

Declare the function fread () in C

Below is the declaration for the fread () function in C:

 size_t fread ( void * ptr , size_t size , size_t nmemb , FILE * stream ) 

Parameters

ptr - This is a pointer to a memory block with a minimum size of size * nmemb bytes.

size - This is the size (byte value) of each element to be read.

nmemb - This is the number of elements, with each element having the size of byte size.

stream - This is the pointer to a FILE object that defines an Input Stream.

Returns the value

The total number of successfully read elements is returned as a size_t object, which is an integer data type. If this number is different from the nmemb parameter, then an error has occurred or End-Of-File has been encountered.

For example

The following program C illustrates the usage of the fread () function in C:

 #include #include int main () { FILE * fp ; char c [] = "Hoc C co ban va nang cao tai QTM !!!" ; char buffer [ 100 ]; /* mo file de doc va ghi */ fp = fopen ( "baitapc.txt" , "w+" ); /* Ghi du lieu vao file */ fwrite ( c , strlen ( c ) + 1 , 1 , fp ); /* thiet lap vi tri con tro tim kiem ve dau file */ fseek ( fp , SEEK_SET , 0 ); /* Doc va hien thi du lieu */ fread ( buffer , strlen ( c )+ 1 , 1 , fp ); printf ( "%sn" , buffer ); fclose ( fp ); return ( 0 ); } 

Compile and run the above C program to see the results:

According to Tutorialspoint

Previous lesson: Function fopen () in C

Next lesson: Function freopen () in C

4 ★ | 1 Vote

May be interested

  • Function freopen () in CPhoto of Function freopen () in C
    function file * freopen (const char * filename, const char * mode, file * stream) in library c attaches a new filename with the given stream and at the same time closes the old file in stream.
  • Function fseek () in CPhoto of Function fseek () in C
    the function int fseek (file * stream, long int offset, int whence) in standard c library sets the stream file location to the given offset. the offset parameter specifies the number of bytes to search from where the given location is.
  • The function fsetpos () in CPhoto of The function fsetpos () in C
    the function int fsetpos (file * stream, const fpos_t * pos) in library c sets the file location of the stream to the given location. the pos parameter is a location provided by the fgetpos function.
  • Function ftell () in CPhoto of Function ftell () in C
    the function long int ftell (file * stream) in standard c library returns the current file location of the given stream.
  • Function fwrite () in CPhoto of Function fwrite () in C
    function size_t fwrite (const void * ptr, size_t size, size_t nmemb, file * stream) in standard c library writes data from the array pointed by ptr to the given stream.
  • Union in CPhoto of Union in C
    a union is a special data in c language that allows you to reserve different data types in the same memory. you can define union with a lot of parameters, but only one component contains values ​​at a time. union provides an effective way to use a device for multiple purposes.