将字符串写入文件末尾(C ++) [英] Writing a string to the end of a file (C++)

查看:115
本文介绍了将字符串写入文件末尾(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经形成了一个程序,该程序具有要流式传输到现有文本文件末尾的字符串.我所拥有的全部是:(C ++)

I have a program already formed that has a string that I want to stream to the end of an existing text file. All of what little I have is this: (C++)

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

我意识到有很多要添加的内容,如果我似乎要求人们为我编写代码,我深表歉意,但是我完全迷失了,因为我以前从未做过这种类型的编程.

I realize there is much to be added to this and I do apologize if it seems I am asking people to code for me, but I am completely lost because I have never done this type of programming before.

我尝试过互联网上使用的不同方法,但这是最有效的方法,并且有些熟悉.

I have attempted different methods that I have come across the internet, but this is the closest thing that works and is somewhat familiar.

推荐答案

使用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 ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆