The basic course of C language (iii) input and output functions and control flow statements (11)

Source: Internet
Author: User
Tags integer numbers printf
The following program writes a floating-point number group to the file Test_b.dat in binary form.
Example 14:
#include <stdio.h>
Main ()
Float f[6]={3.2,-4.34, 25.04, 0.1, 50.56, 80.5};
/* Define floating-point number groups and initialize/*
int i;
Fp=fopen ("Test_b.dat", "WB"); /* Create a binary file only write/*
Fwrite (f, sizeof (float), 6, FP);//* 6 floating-point numbers are written to file/*
Fclose (FP); /* Close File/*
The following example reads 100 integer numbers from the Test_b.dat file and places them in the DAT array.
Example 15:
#include <stdio.h>
Main ()
int dat[100];
Fp=fopen ("Test_b.dat", "RB");/* Open a binary file read only */
if (DAT, sizeof (int), fread, FP)!=100)
/* Judge whether read 100 number/
if (feof (FP))
printf ("End of File"); /* Less than 100 files end of document * *
printf ("Read error"); /* Reading Error * *
Fclose (FP); /* Close File/*
When the file is read and written using standard file functions, the read-write function is first put into the buffer, that is, it operates only on the output buffer, and the reading function only operates on the input buffer. For example, the contents of the buffer are written to a file and the content is first placed in the output buffer until the output buffer is full or the fclose () function is used to close the file. Without the fclose () function, the written content is not saved to the file or the contents of the file written are not complete. There is a function that refreshes the buffer, that is, fflush (), which is called in the form of:
int fflush (FILE *stream);
This function actually writes the contents of the output buffer to the file and clears the contents of the input buffer.
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: 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.