【问题标题】:data parsing using c++使用 C++ 进行数据解析
【发布时间】:2014-08-28 06:19:19
【问题描述】:
.i 8 .o 8 .ilb a b c d e f g h .ob a b c d e f g h 00000000 00000000 00000001 00000011 ...... ...... .e

我试过这样的代码。没有编译错误

#include <string>
#include <iostream> 
#include <sstream>
#include <vector>
#include <bitset>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include "Line12.hpp"
#include "Line34.hpp"
#include "Line261.hpp"

using namespace std;
void printLine12(Line12 line1)
{ 
    cout << "first:" << line1.getFirst() << endl;
    cout << "in:" << line1.getIn() << endl;       
}

int main(int argc, const char * argv[])
{
    std::vector<Line12> lines12;
    std::vector<Line34> lines34;
    std::vector<std::vector<std::bitset<8> > > a;
    std::vector<Line261> lines261;

    std::ifstream in("C:/Users/Lenovo/Desktop/hwb8_64.pla");
    std::string Line;

    for(int i=1; i<=261;i++)
    {
        std::getline(in,Line);
        if(i==1 || i==2)
        {
            Line12 s(Line);
            lines12.push_back(s);
        }
        else if(i==3 || i==4)
        {
            Line34 s1(Line);
            lines34.push_back(s1);
        }
        else if(i==261)
        {
            Line261 s2(Line);
            lines261.push_back(s2);
        }
        else
        {
            a.push_back(std::vector< std::bitset<8> >());
            std::istringstream iss(Line);
            std::string bits;
            while (iss >> bits)
            {
                a.back().push_back(std::bitset<8>(bits));
            }

        }

    }
    system ("PAUSE");
    for(int i=1; i<=261;i++)
    {
        if(i==1 || i==2)
        {
            Line12 s1 = lines12.at(i);
            printLine12(s1);
        } 
        else if(i==3 || i==4)
        {
            Line34 s2 = lines34.at(i);
            printLine34(s2);
        }
        else if(i==261)
        {
            Line261 s3 = lines261.at(i);
            printLine261(s3);
        }    
        else
        {
            for (int x = 0; x < a.size(); ++x)
            {
                for (int y = 0; y < a[i].size(); ++y)
                {
                    for (int z = 7; z >= 0; --z)
                    {
                        std::cout << a[x][y][z];
                    }
                    std::cout << " ";
                }
                std::cout << std::endl;
            }
        }

    }      
    system ("PAUSE");
    return 0;
}

但是当我运行代码时。它显示为这样。我应该怎么做才能纠正这个问题。

  terminate called after throwing an instance of 'std::invalid_argument'

  what<>:  bitset::_M_copy from ptr  

我应该怎么做才能纠正这个错误并得到输出。

【问题讨论】:

    标签: c++ file-io vector


    【解决方案1】:

    bitset 构造函数接受一个由 1 和 0 组成的 string。如果您传递的不是 1 和 0,它会引发 invalid_argument 异常。你确实传递了一些不仅仅是 1 和 0 的东西,而且你在这里。

    在此处发布之前,请自行进行一些基本调试以验证您的程序是否正常工作。

    【讨论】:

    • 我能知道我在代码中是否出错了。我确实将字符串位传递给bitset
    • @user3898956 不,你没有。还是你调试过?你知道这个字符串中有哪些字符吗?您的计算机说它不仅是 1 和 0。我更相信你的电脑,而不是相信你能够以某种方式感知可能发生了什么。学习使用调试器。正确使用调试器将解决这个问题以及您未来的许多问题。
    猜你喜欢
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    相关资源
    最近更新 更多