C++ 新手 : using stream for writing data to file. .. 为什么我的代码引入回车?

标签 c++ string stream streaming

我已经编写了执行计算的代码。代码中有一个循环。每个循环对应不同的时间。

对于每个循环,我想将一个字符串写入外部文件。该字符串应包含“filename_”和循环编号——例如:

'fileName_4'

问题是它显示为(在下一行结束 '):

'文件名_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();

最佳答案

endl 添加新行。希望对你有帮助

关于C++ 新手 : using stream for writing data to file. .. 为什么我的代码引入回车?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6328838/

相关文章:

c++ - 减少调试符号的占用空间(可执行文件膨胀到 4 GB)

c++ - 任务在删除其中一个迭代对象时会导致段错误

javascript - 将 Node.js 缓冲区转换为字符串,然后转换为 JSON

stream - RxJS 可以以基于拉动的方式使用吗?

Node.JS 数据输入流

c++ - 模板 vector 到 vector 转换函数

c++ - 试图对两个编译时元组整数序列求和

c - 将字符串传递给 init 函数并将其存储在 malloc 结构中

java - 角色交换

c# - 无法将字符串解析为整数值