C++新手:使用流将数据写入文件...为什么我的代码引入了回车符?
我已经编写了用于执行计算的代码。代码中有一个循环。每个循环对应不同的时间。
对于每个循环,我想将一个字符串写入外部文件。该字符串应包含“filename_”和循环编号 - 例如:
'fileName_4'
问题是它显示为(在下面的行上带有结束'):
'fileName_4
’
如果有人能提供帮助,我将非常感激。这是我尝试过的:
std::string convertedToString;
std::stringstream numberConverted;
storeNumberForConversion << time << endl; // time is a number, like the 4 in the example above
convertedToString = numberConverted.str() += "'";
fileNameHighestTimeStream.open ("fileName.txt", ios::out | ios::app );
fileNameHighestTimeStream << "'fileName_" << convertedToString << endl;
fileNameHighestTimeStream.close();
我也尝试过:
storeNumberForConversion << time << endl; // time is a number, like the 4 in the example above
convertedToString = numberConverted.str();
fileNameHighestTimeStream.open ("fileName.txt", ios::out | ios::app );
fileNameHighestTimeStream << "'fileName_" << convertedToString << "'" << endl;
fileNameHighestTimeStream.close();
I have written code for performing calculations. There is a loop in the code. Each loop corresponds to a different time.
For each loop, I want to write a string to an external file. The string should contain "filename_" and the number of the loop--for example:
'fileName_4'
The problem is that it is appearing as (with the closing ' on the line below):
'fileName_4
'
If anyone could help, I would be very grateful. Here is what I have tried:
std::string convertedToString;
std::stringstream numberConverted;
storeNumberForConversion << time << endl; // time is a number, like the 4 in the example above
convertedToString = numberConverted.str() += "'";
fileNameHighestTimeStream.open ("fileName.txt", ios::out | ios::app );
fileNameHighestTimeStream << "'fileName_" << convertedToString << endl;
fileNameHighestTimeStream.close();
I have also tried:
storeNumberForConversion << time << endl; // time is a number, like the 4 in the example above
convertedToString = numberConverted.str();
fileNameHighestTimeStream.open ("fileName.txt", ios::out | ios::app );
fileNameHighestTimeStream << "'fileName_" << convertedToString << "'" << endl;
fileNameHighestTimeStream.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
endl 添加新行。希望有帮助
The endl adds the new line. Hope it helps
storeNumberForConversion <<时间<< endl;
您应该删除末尾的
endl
。storeNumberForConversion <<时间;
storeNumberForConversion << time << endl;
You should remove
endl
at the end.storeNumberForConversion << time;
据推测,这是一个拼写错误,并且您有:
您将
endl
流式传输到numberConverted
,因此它包含换行符。简单的!来自您在其他地方的评论:
<代码><< endl 与
<< 相同'\n' <<冲洗
。所以你仍然可以用<< 自己冲洗。刷新
,但实际上通常您应该将其留给流对象在其自己的时间处理。另外,使用
+=
也是值得怀疑的(尽管在这种情况下它恰好产生了正确的结果)。固定的:
Presumably this is a typo, and you have:
You streamed
endl
tonumberConverted
, so it contains a newline. Simple!From your comment elsewhere:
<< endl
is the same as<< '\n' << flush
. So you can still do the flushing yourself with<< flush
, but actually usually you should just leave this up to the stream object to handle in its own time.Also the use of
+=
is suspect (though it happens to yield the correct result in this case).Fixed: