使用c++进行数据解析

data parsing using c++

本文关键字:数据 c++ 使用      更新时间:2023-10-16
.i 8.o 8.ilb a b c d e f g h.ob a b c d e f g h00000000000000000000000 1 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  

我应该做些什么来纠正这个错误并得到输出。

bitset构造函数接受一个1和0的string。如果您传递的不是1和0的东西,它将抛出invalid_argument异常。你确实通过了一些不仅仅是1和0的东西,现在你来了。

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