gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:33:02 27 4
gpt4 key购买 nike

我有一个程序已经形成,它有一个字符串,我想将它流式传输到现有文本文件的末尾。我所拥有的只是:(C++)

 void main()
{
std::string str = "I am here";
fileOUT << str;
}

我意识到还有很多要补充的,如果我似乎在要求人们为我编写代码,我深表歉意,但我完全迷失了,因为我以前从未做过这种类型的编程。

我尝试了在互联网上遇到的不同方法,但这是最接近有效且有点熟悉的方法。

最佳答案

使用 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;

关于c++ - 将字符串写入文件末尾(C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6932409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com