文件模式:
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");
}