一、打开与关闭
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++语言中的文件的输入输出,包括文件流、文件操作等。 * 字符串处理:介绍了 C++语言中的字符串处理,包括字符串的操作、字符串的格式化等。 * 容器类:介绍了 C++语言中的容器类,包括 ...
第六章讲述了C++标准函数库,包括文件输入/输出、字符串处理、容器类、算法等。 学习建议 为了更好地学习C++,读者可以根据自己的需求选择学习章节,并且可以快速阅读前三章的内容,因为这些内容主要介绍了C++的...
在C++程序中,可以通过Win32 API提供的函数来访问和操作ini文件。ini文件是一种配置文件格式,通常用于存储程序的配置信息,它的结构由多个小节(sections)组成,每个小节下面可以包含多个键值对(key-value pairs...
特别提到,对C语言有所了解的读者,可以将教程的前几章作为复习,因为这部分重点介绍C++中的C语言元素。然而,由于C++与C存在细微差异,即便是熟悉C语言的读者,也应快速浏览这部分内容,确保掌握所有关键点。 三、...
对C语言有一定了解的读者可以将教程的前三章(1.1到3.4)当作复习,因为这部分内容主要介绍C++中的C部分。然而,C++的语法与C存在一些差异,因此建议即使是C语言的熟悉者也要快速浏览这些章节。 接下来,教程的主体...
- **文件的输入输出**:讲解如何使用C++标准库中的流类(如ifstream、ofstream等)来进行文件读写操作。 通过上述知识点的详细介绍,本教程旨在帮助初学者快速掌握C++的基础知识,同时也能为有一定编程经验的学习者...
- 在本章前面的小节中,学生已经学习了运算符重载的基本概念、方法和规则,具备进一步学习重载为成员函数和友元函数的能力。 - **学生思想状况分析**: - 强调课程的重要性和学习的意义,引导学生认识到大学学习对...
1. **C++程序结构**:这部分介绍了C++程序的基本组成部分,如源文件、函数定义、主函数main()等,让读者了解一个C++程序是如何组织和运行的。 2. **变量和数据类型**:讲解了变量的声明、初始化以及C++中的基本数据...
熟悉C语言的读者可以从第4章开始,因为前三章主要介绍C++中与C语言相似的部分,但仍然建议快速浏览以了解C++的独特之处。 2. **C++基础**: - **程序结构**:讲解C++程序的基本结构,包括源文件、头文件、函数定义...
7. **文件操作**:C++提供了丰富的文件操作接口,读者可以通过学习这部分内容,了解如何读写文件,进行数据持久化存储。 8. **实战项目**:书中的例程和课后题是实践理论知识的好机会,通过这些练习,读者可以提升...
1. **C++基础**:文件可能包含变量声明、数据类型、运算符、流程控制(如if-else、for、while)、函数定义和调用等基础知识。 2. **面向对象编程**:作为C++的核心特性,面向对象编程(OOP)在这些代码中会体现为类...
#### 八、C++函数高级特性 **1. 函数重载** - **重载概念**:同一函数名可以通过参数列表的不同来区分。 - **成员函数重载**:在类中可以重载成员函数。 **2. 运算符重载** - **运算符重载**:通过运算符重载使得...
9.9 在派生类中使用构造函数和析构函数 9.10 将派生类对象隐式转换为基类对象 9.11 关于继承的软件工程 9.12 复合与继承的比较 9.13 对象的“使用”关系和“知道”关系 9.14 实例研究:类Point、CircIe和...
- **文件的输入输出**:介绍了如何使用C++标准库进行文件操作,包括文件流对象和文件操作函数。 教程结构清晰,每个章节分为多个小节,方便读者按需学习。教程鼓励读者通过修改示例程序来加深理解和实践,这是掌握...
从提供的压缩包文件名称列表可以看出,习题涵盖了多个章节,比如“3-4.cpp”可能是第三章第四题的解答,“4_1_16.cpp”可能是第四章第一小节第十六题的答案。这些题目可能涉及了C++的基础语法,例如变量、数据类型、...
9.9 在派生类中使用构造函数和析构函数 9.10 将派生类对象隐式转换为基类对象 9.11 关于继承的软件工程 9.12 复合与继承的比较 9.13 对象的“使用”关系和“知道”关系 9.14 实例研究:类Point、CircIe和...
这个压缩包中的文件名暗示了它们可能是不同章节的习题答案,包括了一些特定的题目编号,如7-3-(2).cpp、5-3-(5).cpp等,这通常表示这些源代码文件是对应教材第7章第3小节的第2个问题,或者是第5章第3小节的第5个问题...
10.2.3 OpenGL辅助函数库 10.3 操作步骤 10.4 创意与超越 10.5 本章小结 第11章 二人对战五子棋 11.1 基础知识 11.1.1 游戏界面设计 11.1.2 游戏所使用的数据结构和算法 11.1.3 ...