A brief introduction to the correlation function usage of C language reading file stream _c language

Source: Internet
Author: User
Tags fread int size

C language Fread () function: Read file function (read data from file stream)

Header file:

#include <stdio.h>

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:

#include <stdio.h>

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.