为什么getline不能正确读取文档的内容?


// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include<sstream>
#include<string>
#include <fstream>
#include<algorithm>
#include <vector>
using namespace std;

int main()
{
    ifstream ifs;
    ifs.open("./text.txt", ios::in);
    if (!ifs.is_open())
    {
        cout << "open error" << endl;
        return -1;
    }
    string temp;
    string str;
    vector<vector<string>> i_content;
    while (getline(ifs,temp))
    {
        vector<string> i_temp;
        istringstream ifst(temp);
        ifst >> str >> str >> str >> str>>str>>str;//用第6列来和"nn"来比较。
        if (str == "nn")
        {
            auto it = temp.begin();
            while (it != temp.end())//因为istringstream会跳过"",所以添加该步骤。
            {
                if (*it == *(++it) == '/t')
                {
                    temp.insert(it, 1, 'N');//用N来表示这个是null的""
                }
            }
            istringstream istr1(temp);
            while (istr1 >> str)//把该行全部一 一添加进去。
            {
                i_temp.push_back(str);
            }
            i_content.push_back(i_temp);
        }
    }
    return 0;

    
}

结果如图

img

文档如图

img

你这第6列也不是nn啊

读取到的第一行不一样且在读一次直接结束了

你的文本文件,存的是文本吗?


可以查看手册:c++-getline<>() 中的内容