Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

A small step every day, a big step to success. Hello everyone, I'm GW_gw, and I'm happy to learn daily trivia with you.Copy the code

The following content is from the network, if there is any infringement, please contact me to delete, this article is only used for learning exchange, not for any commercial purposes.

Abstract

This paper mainly describes some basic operations of C language file reading, text file reading, binary file reading.Copy the code

Written to the file

4.1 Writing a text file

 int fputc( int c, FILE *fp );
Copy the code

Writes a single character to a file. Returns the written character on success or EOF on failure.

 int fputs( const char *s, FILE *fp );
Copy the code

Writes a string to a file, returning a non-negative value on success or EOF on failure.

 int fprintf(FILE *fp,const char *format, ...)
Copy the code

Sends formatted output to a file. For the string format, see printf.

4.2 Writing binary Files

 size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
Copy the code

PTR refers to the memory block of the parameter being written.

Size Specifies the size of each element to be written

Nmemb Number of elements written

Stream points to the file being written

Read nmemb size numbers from the PTR into the file pointed to by the stream.

conclusion

The above is the text file and binary file write operation, since the C language file read and write series here, welcome everyone to correct.

Welcome to click on portal to view file read and write series:

C Language — Nanny teaching, document reading and writing (I)

C Language — Nanny teaching, document reading and writing (2)