有没有办法在阅读文本时使用右移来识别'n'

Is there a way to recognize ' ' while using right shift on reading text

本文关键字:右移 识别 文本 有没有      更新时间:2023-10-16

这是我的文本文件格式

勒尼汉于1995年退休,担任社会学副教授

在约翰杰伊刑事司法学院。他已经加入了教师队伍

1980年,在哥伦比亚大学担任研究员之后

这是我的代码

ifstream  afile("sometext.txt");
string line;
while (afile >> line) {
    cout<< line <<" "
    }
afile.close();

,打印时没有任何新行

是否可以在字符串上只使用右移打印新行?

你可以这样做,但使用字符而不是字符串,使用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;
}