我不能添加一个新的行到c ++字符串 [英] I can't add a new line to c++ string

查看:168
本文介绍了我不能添加一个新的行到c ++字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c ++字符串中添加新行?我正在尝试读取一个文件,但是当我尝试附加'\'它不起作用。

How do you add a new line to a c++ string? I'm trying to read a file but when I try to append '\n' it doesn't work.

std::string m_strFileData;
while( DataBegin != DataEnd ) {
    m_strFileData += *DataBegin;
    m_strFileData += '\n';
    DataBegin++;
}


推荐答案

如果你有很多要处理的行,使用 stringstream 可能会更有效率。

If you have a lot of lines to process, using stringstream could be more efficient.

ostringstream lines;

lines << "Line 1" << endl;
lines << "Line 2" << endl;

cout << lines.str();   // .str() is a string

输出:

Line 1
Line 2

这篇关于我不能添加一个新的行到c ++字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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