一、打开与关闭
1.1fopen
FILE * fopen ( const char * filename, const char * mode );
打开文件。即采用mode方式,打开filename文件。
其中,mode有如下形式:
"r" |
Open a file for reading. The file must exist. |
"w" |
Create an empty file for writing. If a file with the
same name already exists its content is erased and the file is treated
as a new empty file. |
"a" |
Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. |
"r+" |
Open a file for update both reading and writing. The file must exist. |
"w+" |
Create an empty file for both reading and writing.
If a file with the same name already exists its content is erased and
the file is treated as a new empty file. |
"a+" |
Open a file for reading and appending. All writing
operations are performed at the end of the file, protecting the previous
content to be overwritten. You can reposition (fseek
, rewind
)
the internal pointer to anywhere in the file for reading, but writing
operations will move it back to the end of file. The file is created if
it does not exist. |
返回值:
成功:文件流指针
失败:NULL
1.2 freopen
FILE * freopen ( const char * filename, const char * mode, FILE * stream );
以不同的打开方式打开文件。
返回值:
成功:流指针
失败:NULL
1.3 fclose
int fclose ( FILE * stream );
关闭文件。即关闭之前打开的stream。
返回值:
成功:0
失败:EOF
二. 读
2.1 fread
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
从stream(文件流)中读取count个大小为size的数据到ptr中。即从文件的当前定位位置开始,读取size*count的数据,存放在ptr指向的内存中。
返回值:读取到的块数。如果返回结果与count不相等,那么可能出现了:读取错误 or 读到文件尾部。
You can use either ferror
or feof
to check whether an error happened or the End-of-File was reached.
2.2 fgetc
int fgetc ( FILE * stream );
从流中读取一个字符。
返回值:
成功:字符
失败:EOF(可能是读到文件的尾部或者出错)
2.3 fgets
char * fgets ( char * str, int num, FILE * stream );
从stream中读取字符串。正常情况下,从Stream中读取num-1个字符。如果遇到EOF、换行,就结束读取。
返回值:
成功:str
失败:NULL
2.4 fscanf
int fscanf ( FILE * stream, const char * format, ... );
读取格式化的数据。
返回值:
成功:个数(读取成功的)
失败:EOF
三.写
3.1 fwrite
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
写块数据到流中。将ptr指向的count个size大小的数据写入到sttream。
返回值:
成功:count
失败:!=count
3.2 fputc
int fputc ( int character, FILE * stream );
把字符写入stream。写入一个字符,并将stream定位到原来字符位置(即后移一位)
返回值:
成功:character
失败:EOF
3.3 fputs
int fputs ( const char * str, FILE * stream );
将str指定的字符串写入stream。
返回值:
成功:非负数
失败:EOF
3.4 fprintf
int fprintf ( FILE * stream, const char * format, ... );
将格式化数据,写入文件。
返回值:
成功:写入的字节数
失败:负数
四、定位
4.1 fseek
int fseek ( FILE * stream, long int offset, int origin );
重定位流指示器。在stream中,以origin为基准,偏移offset。
其中,offset整数表示正向偏移,负数表示负向偏移。origin可选择如下三值:
SEEK_SET |
Beginning of file |
SEEK_CUR |
Current position of the file pointer |
SEEK_END |
End of file |
返回值:
定位成功:0
不成功 :非0
4.2 fgetops
int fgetpos ( FILE * stream, fpos_t * position );
获取stream的当前位置。
其中,postion是存储流位置的结构数据。
返回值:
成功:0
失败:!0
4.3 fsetops
int fsetpos ( FILE * stream, const fpos_t * pos );
设置stream的位置指示器。
返回值:
成功:0
失败:!0
/* fsetpos example */
#include <stdio.h>
int main ()
{
FILE * pFile;
fpos_t position;
pFile = fopen ("myfile.txt","w");
fgetpos (pFile, &position);
fputs ("That is a sample",pFile);
fsetpos (pFile, &position);
fputs ("This",pFile);
fclose (pFile);
return 0;
}
五、其他
5.1 fflush
int fflush ( FILE * stream );
清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件。
返回值:
成功:0
失败:EOF
/* FFLUSH.C */
#include <stdio.h>
#include <conio.h>
void main( void )
{
int integer;
char string[81];
/* Read each word as a string. */
printf( "Enter a sentence of four words with scanf: " );
for( integer = 0; integer < 4; integer++ )
{
scanf( "%s ", string );
printf( "%s\n ", string );
}
/* You must flush the input buffer before using gets. */
fflush( stdin );
printf( "Enter the same sentence with gets: " );
gets( string );
printf( "%s\n ", string );
}
5.2 feof
int feof ( FILE * stream );
检测文件流指示位置是否在尾部。
返回值:
在 :!0
不在:0
5.3 ferror
int ferror ( FILE * stream );
错误检测。
返回值:
有错误:!0
没错误:0
参考资料:
1. stdio.h
分享到:
相关推荐
对于C语言熟悉的读者可将前三章(1.1 到 3.4)当作复习,因为这部分内容主要介绍C++中的C部分。不过某些C++的语法与C还是有些差别,所以建议还是快速的读一下这部分。 第四章讲述面向对象编程。 第五章主要介绍ANSI-...
9.9 在派生类中使用构造函数和析构函数 9.10 将派生类对象隐式转换为基类对象 9.11 关于继承的软件工程 9.12 复合与继承的比较 9.13 对象的“使用”关系和“知道”关系 9.14 实例研究:类Point、CircIe和...
9.9 在派生类中使用构造函数和析构函数 9.10 将派生类对象隐式转换为基类对象 9.11 关于继承的软件工程 9.12 复合与继承的比较 9.13 对象的“使用”关系和“知道”关系 9.14 实例研究:类Point、CircIe和...
10.2.3 OpenGL辅助函数库 10.3 操作步骤 10.4 创意与超越 10.5 本章小结 第11章 二人对战五子棋 11.1 基础知识 11.1.1 游戏界面设计 11.1.2 游戏所使用的数据结构和算法 11.1.3 ...
1.3 在Visual C++中处理数字图像 22 1.3.1 位图和调色板 22 1.3.2 图形设备接口 23 1.3.3 OpenCV 26 1.4 本章小结 26 第2章 Visual C++ 2005基础知识 27 2.1 利用向导生成应用程序 28 2.1.1 创建新项目 28 2.1.2 ...
第0条 不要拘泥于小节(又名:了解哪些东西不应该标准化) 2 第1条 在高警告级别干净利落地进行编译 4 第2条 使用自动构建系统 7 第3条 使用版本控制系统 8 第4条 做代码审查 9设计风格 11 第5条 一...
采用了很多便于巩固所学知识的设计,例如各章开头的小节总览,书中随处可见的小结框、编程提示和编程陷阱,各章结尾的小结、习题、编程练习和编程项目等。这些非常适合初学者掌握重要的编程概念。全书共18章,8个...
采用了很多便于巩固所学知识的设计,例如各章开头的小节总览,书中随处可见的小结框、编程提示和编程陷阱,各章结尾的小结、习题、编程练习和编程项目等。这些非常适合初学者掌握重要的编程概念。全书共18章,8个...
C++101条标准规范 C++ Coding Standards Herb Sutter著 组织和策略问题 第0条 不要拘泥于小节(又名:了解哪些东西不应该标准化 只规定需要规定的事情,不要强制施加个人喜好或者过时的做法。 第1条 在高警告级别...
本章适用于CGAL新手。学习CGAL之前,需要对C++语言、几何算法有...在第二小节,读者将会看到二维凸包函数是如何获取输入及如何输出结果的。第三小节将解释使用Traits 类的原因。第四小节将解释concept和model的定义。
在c中,申请动态内存是使用malloc和free,这两个函数是c的标准库函数,分配内存使用的是系统调用,使用它们必须包含stdlib.h,才能编译通过。 malloc后需要检查内存是否分配成功,free则要在指针不为空的情况下才能...
一个基于 C++11 的类,它基于Philip J. Schneider 于 1990 年在Graphics ...以下小节描述了函数列表及其预期行为。 构造函数 构造函数不需要任何实现,但它需要为使用的模板提供参数列表。在 的情况下OsgPathFitter,
primer第11.3.6小节中出现的单词置换程序,用到了文件输入输出流、字符流、字典 character.cpp : 字符串操作 C++中的字符容器string; C风格的字符串; C风格字符串的库函数; 字符串相关的笔试题; my_thread.cpp : ...
1.7 C语言与C++的历史--------------------------------------------------6 1.8 C++标准库---------------------------------------------------------7 1.9 Java、Internet与万维网-------------------------...
东北大学软件学院信息安全校内实训 题目:编写一个最简单的控制台程序,编译成Release版本,使用OllyDbg分析该程序,图文说明main函数...自己创建或收集几个C++程序,根据2.1小节的内容图文形式说明main函数出现的标志
这是c ++中进度条的简单库。 examples / example.cpp的输出: 说明文件: 初始化进度条ProgressBar name(char notDoneChar, char doneChar,unsigned int size) ,notDoneChar是开始执行流程之前将填充的进度条,...