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

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

问题描述

所以我有一个程序已经形成了一个字符串,我想流到一个现有的文本文件的末尾。所有我所拥有的是这样的:(C ++)

pre $ $ $ $ $ $ $
$ :: string str =我在这里;
fileOUT<<海峡;





我知道还有很多东西要添加到这里,似乎我在问人们为我编码,但我完全失去了,因为我以前从来没有做过这种类型的编程。



我尝试了不同的方法,我已经来了通过互联网,但这是最接近的作品,是有点熟悉。

解决方案使用<$ c $打开您的文件c> std :: ios :: app

  #include< fstream> 

std :: ofstream out;

// std :: ios :: app是开放模式append,意思是
//新的数据将被写入文件的末尾。
out.open(myfile.txt,std :: ios :: app);

std :: string str =我在这里。
out<<海峡;


So 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.

解决方案

Open your file using 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天全站免登陆