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 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题