Fread (), fwrite () read/write stream

Source: Internet
Author: User
Tags fread

C Library Function-FRead ()Describe

The C library function size_t fread (void *ptr, size_t size, size_t nmemb, FILE *stream) reads data from the given stream stream to the ptr reference to the array.

Statement

The following is a declaration of the Fread () function.

size_t fread (void *ptr, size_t size, size_t nmemb, FILE *stream)

Parameters

    • PTR -This is a pointer to a block of memory with a minimum size of size*nmemb bytes.
    • Size-This is the amount of each element to be read, in bytes.
    • Nmemb -This is the number of elements, each of which is a size byte.
    • Stream -This is a pointer to the file object that specifies an input stream.
return value

The total number of elements successfully read is returned as an size_t object, and the Size_t object is an integer data type. If the total is different from the NMEMB parameter, an error may have occurred or the end of the file has been reached.

Instance

The following example demonstrates the use of the Fread () function.

#include <stdio.h>#include<string.h>intMain () {FILE*FP; CharC[] ="This is the new World"; Charbuffer[ -]; /*Open file for read-write*/FP= fopen ("file.txt","w+"); /*writing data to a file*/fwrite (c, strlen (c)+1,1, FP); /*find the beginning of a file*/fseek (FP, Seek_set,0); /*reading and displaying data*/fread (buffer, strlen (c)+1,1, FP); printf ("%s\n", buffer);      Fclose (FP); return(0);}

Let's compile and run the above program, which will create a file file.txtand then write the contents of this is W3cschool. Next we use the fseek () function to reset the write pointer to the beginning of the file, as shown in the following file:

This is the New World
C Library Function-fwrite ()Describe

The C library function size_t fwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream) writes data from the array pointed to by PTR to the given stream Stream .

Statement

The following is a declaration of the fwrite () function.

size_t fwrite (constvoid *ptr, size_t size, size_t nmemb, FILE *stream)

Parameters
    • PTR -This is a pointer to an array of elements to be written.
    • Size-This is the amount of each element to be written, in bytes.
    • Nmemb -This is the number of elements, each of which is a size byte.
    • Stream -This is a pointer to the file object that specifies an output stream.
return value

If successful, the function returns an Size_t object that represents the total number of elements when an integer data type is in the object. If the number differs from the NMEMB parameter, an error is displayed.

Instance

The following example demonstrates the use of the fwrite () function.

 #include <stdio.h>int   main () {FILE    *FP;  char  str[] =  " this is new world.cc  "  ; FP  = fopen ( " file.txt  " ,  " w  "  ); Fwrite (str,  1 , sizeof   (str), FP);     Fclose (FP);  return  (0   

Let's compile and run the above program, which will create a file file.txt, which reads as follows:

This is the New World.  CC   

Now let's use the following program to view the contents of the above file:

#include <stdio.h>intMain () {FILE*FP; intC; FP= fopen ("file.txt","R");  while(1) {C=fgetc (FP); if(feof (FP)) { Break ; } printf ("%c", c);   } fclose (FP); return(0);}

Fread (), fwrite () read/write stream

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.