简体   繁体   English

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

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

my code is this: 我的代码是这样的:

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
fstream file;
file.open("abc.txt",ios::app);
file<<"hello";
file.close();
return 0;
}

The above code is creating an empty file. 上面的代码正在创建一个空文件。

Please can anyone point it out where i'm going wrong. 请任何人指出我要去哪里了。

When you open your file, in addition to specifying append ( ios::app ), you must also specify that you want to output to the file ( ios::out ). 打开文件时,除了指定append( ios::app )外,还必须指定要输出到文件( ios::out )。 You combine them with a bitwise or ( | ) since they represent single bit flags. 您将它们与按位或( | )组合,因为它们表示单个位标志。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM