Current location - Quotes Website - Personality signature - How does the fread () function determine whether the end of the file has been reached?
How does the fread () function determine whether the end of the file has been reached?
Reading data from a file stream

Correlation functions fopen, fwrite, fseek, fscanf

Header file # include

Define the function size _ tfraed (void * ptr, size _ t size, size _ t nmemb, file * stream);

Function description fread () is used to read data from a file stream.

The parameter stream is an open file pointer.

Parameter ptr points to the data space to be stored, and the number of characters to be read is determined by parameter size*nmemb.

Fread () will return the number of nmemb actually read. If the value is less than the parameter nmemb, it means that the end of the file may be read or an error may occur. In this case, you must use feof () or ferror () to determine what happened.

The return value returns the number of nmemb actually read.

additional information

Example # includes

# Define nmemb 3

Structural testing

{

char name[20];

Int size;

} s[nmemb];

Master ()

{

File * stream;

int I;

stream = fopen("/tmp/fwrite "," r ");

fread(s,sizeof(struct test),nmemb,stream);

Fclose (flow);

for(I = 0; I & ltnmembi++)

printf(" name[% d]= %-20s:size[% d]= % d \ n ",I,s[i]。 Name, me, s[i]. Size);

}

Execution name [0]=Linux! Size [0]=6

name[ 1]=FreeBSD! size[ 1]=8

name[2]= windows 2000 size[2]= 1 1

========================

Feof (check whether the file stream has read the end of the file)

Correlation functions fopen, fgetc, fgets, fread

Header file # include

Define the function int feof (file * stream);

The function description feof () is used to detect whether the end of the document has been read, and the mantissa stream is the document pointer returned by fopen (). If the end of the file is reached, a non-zero value is returned, otherwise, 0 is returned.

A return value of non-zero indicates that the end of the file has been reached.