文件模式:
| in | 打开文件做读操作 |
| out | 打开文件做写操作 |
| app | 在每次写之前找到文件尾 |
| ate | 打开文件后立即将文件定位在文件尾 |
| trunc | 打开文件时清空已存在的文件流 |
| binary | 以二进制模式进行IO操作 |
文件模式组合:
| out | 打开文件做写操作,删除文件中已有的数据 |
| out | app | 打开文件做写操作,在文件尾写入 |
| out | trunc | 与out模式相同 |
| in | 打开文件做读操作 |
| in | out | 打开文件做读写、写操作,并定位于文件开头处 |
| in | out | trunc | 打开文件做读、写操作,删除文件中已有的数据 |
例子:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main(){
fstream outfile;
outfile.open("1.txt", fstream::out|fstream::trunc);
for( int i = 0; i < 1000; i++ )
outfile<<i<<' ';
outfile<<endl;
outfile.close();
system("pause");
}
本文详细介绍了C++中文件操作的不同模式,包括基本的读写模式如in、out等,以及组合模式如in|out|trunc等。通过示例代码展示了如何使用这些模式来实现特定的功能。
2540

被折叠的 条评论
为什么被折叠?



