Linux: C language programming: fread

Source: Internet
Author: User

Fread

Function Name: fread
Function: read data from a stream
Usage: int fread (void * PTR, int size, int nitems, file * stream );
Parameter: The address (PTR) used to receive data)
Size of a single element)
Number of elements (nitems)
Provides the data file pointer (Stream)
Returned value: number of successfully read Elements
Program Example:
# Include <string. h>
# Include <stdio. h>
Int main (void)
{
File * stream;
Char MSG [] = "this is a test ";
Char Buf [20];
If (Stream = fopen ("dummy. fil", "W + "))
= NULL)
{
Fprintf (stderr,
"Cannot open output file. \ n ");
Return 1;
}
/* Write some data to the file */
Fwrite (MSG, strlen (MSG) + 1, 1, stream );
/* Seek to the beginning of the file */
Fseek (stream, seek_set, 0 );
/* Read the data and display it */
Fread (BUF, 1, strlen (MSG) + 1, stream );
Printf ("% s \ n", Buf );
Fclose (Stream );
Return 0;
}

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.