C++新手:使用流将数据写入文件...为什么我的代码引入了回车符?

发布于 2024-11-15 07:07:20 字数 1080 浏览 3 评论 0原文

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

对于每个循环,我想将一个字符串写入外部文件。该字符串应包含“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兰花执着 2024-11-22 07:07:20

endl 添加新行。希望有帮助

The endl adds the new line. Hope it helps

叫思念不要吵 2024-11-22 07:07:20

storeNumberForConversion <<时间<< endl;

您应该删除末尾的endl

storeNumberForConversion <<时间;

storeNumberForConversion << time << endl;

You should remove endl at the end.

storeNumberForConversion << time;

带上头具痛哭 2024-11-22 07:07:20
std::string       convertedToString;
std::stringstream numberConverted; 

storeNumberForConversion << time << endl;   // time is a number, like the 4 in the example above
convertedToString = numberConverted.str() += "'";

据推测,这是一个拼写错误,并且您有:

std::string       convertedToString;
std::stringstream numberConverted; 

numberConverted << time << endl;   // time is a number, like the 4 in the example above
convertedToString = numberConverted.str() += "'";

您将 endl 流式传输到 numberConverted,因此它包含换行符。简单的!

来自您在其他地方的评论:

我认为 endl 会刷新流?如果我不包含 endl,我是否应该担心刷新流?如果是这样,如果没有 endl,我该怎么做?

<代码><< endl 与 << 相同'\n' <<冲洗。所以你仍然可以用 << 自己冲洗。刷新,但实际上通常您应该将其留给流对象在其自己的时间处理。

另外,使用 += 也是值得怀疑的(尽管在这种情况下它恰好产生了正确的结果)。

固定的:

std::stringstream numberConverted;
numberConverted << time;

std::string       convertedToString;
convertedToString = numberConverted.str() + "'";
std::string       convertedToString;
std::stringstream numberConverted; 

storeNumberForConversion << time << endl;   // time is a number, like the 4 in the example above
convertedToString = numberConverted.str() += "'";

Presumably this is a typo, and you have:

std::string       convertedToString;
std::stringstream numberConverted; 

numberConverted << time << endl;   // time is a number, like the 4 in the example above
convertedToString = numberConverted.str() += "'";

You streamed endl to numberConverted, so it contains a newline. Simple!

From your comment elsewhere:

I thought the endl would flush the stream? If I DON'T include the endl, should I worry about flushing the stream? If so, how would I do it without endl?

<< 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:

std::stringstream numberConverted;
numberConverted << time;

std::string       convertedToString;
convertedToString = numberConverted.str() + "'";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文