Development of large operating system experiments (8) ----- C function fread

Source: Internet
Author: User

C language library function name: introduction function prototype: size_t fread (void * buffer, size_t size, size_tcount, FILE * stream); Function Ability: read data from a FILE stream, read count elements, each element size byte. if the call is successful, return count. if the call is successful, the actual size * count byte parameter is read: buffer memory address used to receive data, at least size * count bytes. size: the size of a single element. The unit is the number of count elements. Each element is a size byte. stream input stream return value: number of elements actually read. if the returned value is different from count (not count * size), the end of the file may be incorrect. get error information from ferror and feof or check whether it has reached the end of the file. program example # include <stdio. h> # include <string. h> int main (vo Id) {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, 1, strlen (msg) + 1, stream ); /* seek to the beginning of the file */fseek (stream, 0, SEEK_SET);/* read the data and display it */fread (buf, 1, strlen (msg) + 1, stream); printf ("% s \ n ", Buf); fclose (stream); return 0;} MSDN example # include <stdio. h> void main (void) {FILE * stream; char list [30]; int I, numread, numwritten;/* Open file in text mode: */if (stream = fopen ("fread. out "," w + t "))! = NULL) {for (I = 0; I <25; I ++) list [I] = (char) ('Z'-I ); /* Write 25 characters to stream */numwritten = fwrite (list, sizeof (char), 25, stream); printf ("Wrote % d items \ n", numwritten ); fclose (stream);} else printf ("Problem opening the file \ n"); if (stream = fopen ("fread. out "," r + t "))! = NULL) {/* Attempt to read in 25 characters */numread = fread (list, sizeof (char), 25, stream ); printf ("Number of items read = % d \ n", numread); printf ("Contents of buffer = %. 25s \ n ", list); fclose (stream);} else printf (" File cocould not be opened \ n ");}

Related Article

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.