无法在c++中使用追加模式在文件中插入数据

Not able to insert data in file using append mode in c++

本文关键字:文件 模式 插入 数据 追加 c++      更新时间:2023-10-16

我的代码是:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream file;
file.open("abc.txt",ios::app);
file<<"hello";
file.close();
return 0;
}

上面的代码正在创建一个空文件。

谁能指出我错在哪里

打开文件时,除了指定追加(ios::app)外,还必须指定要输出到文件(ios::out)。您可以将它们与位或(|)组合,因为它们表示单位标志。

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    fstream file;
    file.open("abc.txt",ios::app | ios::out);
    file << "hello";
    file.close();
    return 0;
}