lucky3778 2022-06-28 15:55 采纳率: 81.8%
浏览 67
已结题

为什么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

  • 写回答

4条回答 默认 最新

  • 赵4老师 2022-06-28 16:41
    关注

    你这第6列也不是nn啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 7月6日
  • 已采纳回答 6月28日
  • 修改了问题 6月28日
  • 创建了问题 6月28日