将字符串写入文件末尾(C++)

ked*_*ked 16 c++ string streaming file

我已经形成了一个程序,它有一个字符串,我想要流式传输到现有文本文件的末尾.我所拥有的一切都是这样的:(C++)

 void main()
{
   std::string str = "I am here";
   fileOUT << str;
}
Run Code Online (Sandbox Code Playgroud)

我意识到还有很多东西要添加到这里,如果看起来我要求人们为我编码,我会道歉,但我完全迷失了,因为我以前从未做过这种类型的编程.

我尝试过不同的方法,我已经遇到了互联网,但这是最接近的工作,有点熟悉.

Cha*_*had 30

使用打开文件 std::ios::app

 #include <fstream>

 std::ofstream out;

 // std::ios::app is the open mode "append" meaning
 // new data will be written to the end of the file.
 out.open("myfile.txt", std::ios::app);

 std::string str = "I am here.";
 out << str;
Run Code Online (Sandbox Code Playgroud)


Set*_*gie 6

要将内容附加到文件末尾,只需在模式下打开一个带有ofstream(代表文件流)的文件app(代表追加).

#include <fstream>
using namespace std;

int main() {
    ofstream fileOUT("filename.txt", ios::app); // open filename.txt in append mode

    fileOUT << "some stuff" << endl; // append "some stuff" to the end of the file

    fileOUT.close(); // close the file
    return 0;
}
Run Code Online (Sandbox Code Playgroud)