C language Fread () function: Read file function (read data from file stream)
Header file:
To define a function:
size_t fread (void * ptr, size_t size, size_t nmemb, FILE * stream);
Function Description: Fread () is used to read data from the file stream.
The parameter stream is an open file pointer, and the parameter PTR points to the data space to be read in, and the number of characters read is determined by the parameter size*nmemb. Fread () returns the number of Nmemb actually read, if the value is smaller than the parameter nmemb, the representative may have read the end of the file or an error occurred, and then you must use Feof () or ferror () to determine what happened.
Return value: Returns the number of Nmemb actually read.
Example
#include <stdio.h>
#define NMEMB 3
struct test
{
char name[20];
int size;
} S[NMEMB];
Main ()
{
FILE * stream;
int i;
stream = fopen ("/tmp/fwrite", "R");
Fread (s, sizeof (struct test), NMEMB, stream);
Fclose (stream);
for (i = 0; i < nmemb i++)
printf ("name[%d]=%-20s:size[%d]=%d\n", I, S[i].name, I, s[i].size);
}
Perform
name[0]=linux! Size[0]=6
name[1]=freebsd! size[1]=8
name[2]=windows2000 size[2]=11
C language feof () function: Check that the file stream is read to the end of the file
header file:
To define a function:
int feof (FILE * stream);
Function Description: feof () is used to detect whether the end of the file is read, and the mantissa stream is the file pointer returned by fopen (). Returns a value other than 0 if it is at the end of the file, and returns 0.
Return value: Returns a non-0 value representing the end of the file that has reached.