码迷,mamicode.com
首页 > 其他好文 > 详细

c读写文件

时间:2017-07-27 12:39:54      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:模式   tput   ignore   div   rom   each   poi   exist   set   

整理一波c读写文件的API。

fopen

FILE * fopen ( const char * filename, const char * mode );

打开模式有以下几种:

"r" read: Open file for input operations. The file must exist.
"w" write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.
"a" append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseekfsetposrewind) are ignored. The file is created if it does not exist.
"r+" read/update: Open a file for update (both for input and output). The file must exist.
"w+" write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.
"a+" append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseekfsetposrewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.

In order to open a file as a binary file, a "b"character has to be included in the mode string.

fread

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

Reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by ptr.

fwrite

size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

Writes an array of count elements, each one with a size of size bytes, from the block of memory pointed by ptr to the current position in the stream.

 

c读写文件

标签:模式   tput   ignore   div   rom   each   poi   exist   set   

原文地址:http://www.cnblogs.com/gattaca/p/7244104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!