2013-09-22 44 views
0

对于我的项目,我必须覆盖操作符>>方法来读取文本文件中的数字数组。这是我第一次做这些,我很迷茫。到目前为止我的代码看起来像这样。读取一个txt文件

std::istream& operator>>(std::istream& in, bigint array){ 

      bool semi = false ; 

      while(!semi){ 
      if(get() = ';') 
       semi = true ; 
      in <<get(); 
      } 
     return in ; 
    } 

而且文件看起来像这样。

10000000000000000000000000000000000345; 
299793000000 
00000000000000000000067; 
4208574289572473098273498723475; 
28375039287459832728745982734509872340985729384750928734590827098752938723; 
99999999; 99999999; 

当每个新阵列遇到";'时停止。白色空间和边缘线也让我感到困惑。任何帮助将不胜感激谢谢。

+3

您错过了问题 – P0W

+2

'get()=';''最有可能是'get()==';''。之前是作业,后者是比较。虽然这可能不正确。 – Dukeling

+0

[读取txt文件]的可能重复(http://stackoverflow.com/questions/18949872/reading-a-txt-file) – hexacyanide

回答

2

你将要使用

bigint& array 

通过参考取值(或你不可能插入数字读进去)。

此外,你将要使用

char ch; 
in >> ch; 

,而不是in << get()(不编译)。更重要的是,加入错误处理:

if (!(in >> ch)) 
{ 
    // we're in trouble (in.rdstate(), in.eof(), in.fail() or in.bad() to know more) 
} 

如果你想使用in.get(),你应该准备跳过自己的空白(包括换行)。在这里我更喜欢std::istream_iterator,因为它会自动这样做(如果std::ios::skipws标志有效,它是默认的)。

所以这里有一个simplist方法中(主要是假设输入数据是有效的和空白忽略):

#include <vector> 
#include <istream> 
#include <iterator> 

struct bigint 
{ 
    std::vector<char> v; // or whatever representation you use (binary? bcd?) 
}; 

std::istream& operator>>(std::istream& in, bigint& array) 
{ 
    for (std::istream_iterator<char> f(in), l; f != l; ++f) { 
     if (*f>='0' && *f<='9') 
      array.v.push_back(*f - '0'); 
     else if (*f==';') 
      break; 
     else 
      throw "invalid input"; 
    } 

    return in; 
} 

#include <iostream> 
#include <sstream> 

int main() 
{ 
    std::istringstream iss(
      "10000000000000000000000000000000000345;\n" 
      "299793000000\n" 
      "00000000000000000000067;\n" 
      "4208574289572473098273498723475;\n" 
      "28375039287459832728745982734509872340985729384750928734590827098752938723;\n" 
      "99999999; 99999999;\n"); 

    bigint value; 
    while (value.v.clear(), iss >> value) 
     std::cout << "read " << value.v.size() << " digits\n"; 
} 

看到它Live on Coliru

2

有相当多的混乱这里。我只列出一些要点,但即使你解决了这些问题,你也有办法解决。

  1. 你究竟在读什么?你说你正在阅读的数字阵列,但你的代码表示,这

    std::istream& operator>>(std::istream& in, bigint array){ 
    

    我可能是错的,但bigint听起来像一个号码给我。我希望这样的事情

    std::istream& operator>>(std::istream& in, std::vector<bigint>& array){ 
    
  2. 这使我想到第二点,运营商>>预计将修改它的第二个参数,这意味着它不能按值传递,你必须使用一个参考。换句话说,这是错误的

    std::istream& operator>>(std::istream& in, X x){ 
    

    但这是OK

    std::istream& operator>>(std::istream& in, X& x){ 
    
  3. 您正在尝试读取bigints数组,所以你需要一个循环(你有),每次轮循环你会阅读一个bigint。所以你需要一种方法来阅读一个bigint,你有吗?你的问题或代码中没有任何内容表明你有能力阅读bigint,但这显然是至关重要的。因此,如果您还没有任何代码需要阅读bigint,那么您可以在完成这些代码之前就忘掉这个整个练习,所以首先要处理这个问题。当你阅读一个bigint时,只有这样你才能回到阅读bigint数组的问题。

  4. 另一个棘手的部分是停止条件,当您阅读分号(可能以空格开头)时停止。因此,您需要一种方法来读取下一个非空格字符,并且至关重要的是,您需要一种方法来未读它,如果它不是分号。因此,你需要像这样

    if (!(in >> ch) || ch == ';') 
    { 
        // quit, either no more input, or the next char is a semicolon 
        break; 
    } 
    
    in.putback(ch); // unread the last char read 
    // read the next bigint 
    

希望这有助于。