将stringstream内容写入ofstream [英] Writing stringstream contents into ofstream

查看:790
本文介绍了将stringstream内容写入ofstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在按以下方式使用std::ofstream:

I'm currently using std::ofstream as follows:

std::ofstream outFile;
outFile.open(output_file);

然后我尝试将std::stringstream对象传递给outFile,如下所示:

Then I attempt to pass a std::stringstream object to outFile as follows:

GetHolesResults(..., std::ofstream &outFile){
  float x = 1234;
  std::stringstream ss;
  ss << x << std::endl;
  outFile << ss;
}

现在,我的outFile除了垃圾外什么都没有:"0012E708"反复出现.

Now my outFile contains nothing but garbage: "0012E708" repeated all over.

GetHolesResults中我可以写

outFile << "Foo" << std:endl; 

,它将在outFile中正确输出.

关于我在做什么错的任何建议?

Any suggestion on what I'm doing wrong?

推荐答案

您可以执行此操作,而无需创建字符串.它使输出流读出右侧的流内容(可用于任何流).

You can do this, which doesn't need to create the string. It makes the output stream read out the contents of the stream on the right side (usable with any streams).

outFile << ss.rdbuf();

这篇关于将stringstream内容写入ofstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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