文件输入输出的模式

来源:互联网 发布:python快速开发平台 编辑:程序博客网 时间:2024/05/17 22:03


文件模式:

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");}

原创粉丝点击