fstream头文件文件问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 01:10:47
为什么我用#include <fstream> using namespace std;时候,要对已有文件打开,它就会把文件清空啊!!用#include <fstream.h>就不会 。郁闷!!
我 是用 ofstream 来定义对象的 。用fstream 就会打开失败~~

应当都可以。

ofstream -- 输出文件流 class
fstream -- 输入/输出文件流 class

fstream filestr;
filestr.open ("test.txt", fstream::app);
第2个参数:
fstream::app 尾加文件
fstream::in 输入文件
fstream::out 输出文件

ofstream outfile;
outfile.open ("test.txt",ios_base::app);
app -- 输出文件,尾加
ate -- 打开时,文件位置定在文件尾

注意定义文件流对象的参数
ofstream out("file.txt", ios::app) //app参数即尾接, 打开文件后再EOF前开始写入