以下是我的文本文件格式
勒尼汉于1995年退休,任社会学副教授。
约翰·杰伊刑事司法学院。他加入了教员队伍
1980年,在哥伦比亚大学做了一段时间的研究之后
下面是我的代码
ifstream afile("sometext.txt");
string line;
while (afile >> line) {
cout<< line <<" "
}
afile.close();并且它打印时没有任何新的行
是否可以仅使用right shift在字符串上打印新行?
发布于 2016-09-20 22:16:35
您可以这样做,但使用字符而不是字符串,使用peek()并扫描输入缓冲区,无论它是否包含'\n‘:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream afile("sometext.txt");
std::string line;
char c;
while ( !afile.eof())
{
afile >> c;
if('\n' == afile.peek())
{
c = '\n';
line += c;
}
else
line += c;
}
std::cout << line << std::endl;
afile.close();
std::cout << std::endl;
return 0;
}https://stackoverflow.com/questions/39596209
复制相似问题