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

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

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭