使用C++解析数据

问题描述:

 
.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 

我试过这样的代码。没有编译错误使用C++解析数据

#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需要string的1和0。它会抛出一个invalid_argument异常,如果你传递了不是1和0的东西。你确实传递了一些不仅仅是零和现在的东西。

请在发布之前自己做一些基本的调试来验证您的程序是否正常工作。

+0

我能知道我在代码中出错了吗?我没有把字符串传给bitset。 – user3898956 2014-08-28 06:39:09

+2

@ user3898956不,你没有。还是你调试过吗?你知道吗?这个字符串中的字符是什么?你的电脑说它不仅是1和0。我相信你的计算机比我相信你能够以某种方式感知*可能发生的事情。学习使用调试器。正确使用调试器将解决此问题,并解决您未来的许多问题。 – nvoigt 2014-08-28 07:33:19