2013-12-12 22 views
0

我遇到了istringstream不存儲它讀取的值的問題。這裏是我有:istringstream不存儲變量中的任何東西

 if(inputFile.good()){           //Make sure file is open before trying to work with it 
                    //Begin Working with information 
     cout << "\tIn File: " << input << endl; 
     cout << "------------------------------------" << endl; 
     int number_of_lines = 0; 
     std::string line; 
     while (std::getline(inputFile, line)){ 
      ++number_of_lines; 
     } 
     Time times[number_of_lines]; 
     double math[number_of_lines]; 
     std::string input; 
     int hh, mm; 
     for(int loop=0;loop<number_of_lines;loop++){ 
      std::getline(inputFile, input); 
      std::istringstream(input) >> mm >> hh >> math[loop]; 
      cout << "hours = " << hh << endl; 
      times[loop].setTimeHours(hh); 
      times[loop].setTimeMinutes(mm); 
      times[loop].show(); 
      cout << "*" << math[loop] << endl; 
     } 
     std::cout << "Number of lines in text file: " << number_of_lines << "\n" << endl; 
    }else{ 
     cout << "Could not open file!!!" << endl; 
    } 

的文件我讀看起來像這樣:

90 1 3.0 
1 1 100.0 
2 34 5.1 

和輸出,當我運行:

In File: data04.txt 
------------------------------------ 
hours = 0 
Operation To Be Done = 0:2336552*1.15384e-317 
hours = 0 
Operation To Be Done = 0:2336552*1.58101e-322 
hours = 0 
Operation To Be Done = 0:2336552*1.15397e-317 
Number of lines in text file: 3 

任何人都知道爲什麼它不存儲價值?

+1

你檢查的讀操作是成功的? (只是一個修辭問題,當然是由代碼回答) –

+0

嗯,我只是做了一個'cout << input << endl;'以確保輸入中有東西,但它打印了一個空行,所以某些東西不是'沒有正確的工作!大聲笑 – user2990286

+1

是的。這不會被視爲驗證輸入是否成功。類似於'if(std :: getline(inputFile,line))'或'while(std :: getline(inputFile,line))'或'if(std :: istringstream(intput)>> mm >> hh)將算作驗證。 –

回答

2

有這個代碼

  1. 的幾個關鍵問題,它不檢查是否投入是成功的。您需要確保在處理讀取的數據之前驗證輸入操作是否有效。否則會導致隨機數據被處理。
  2. 您首先閱讀到流的末尾,然後希望流魔法重新啓動。這是行不通的。只需閱讀一次流並繼續追加到std::vector<Time>(或類似的容器)。除了僅遍歷一次文件外,在UNIX上,文件大小可以在讀取時更改。
  3. C++沒有可變大小的數組,儘管某些編譯器可能會提供類似於C的可變大小數組的擴展。在C++中,您將使用std::vector<Time>
0

首先,你的程序是錯誤的。在while循環結束後,文件中沒有更多內容要讀取(除非您將seekg()恢復爲開頭),因此for循環體中的std::getline()調用基本上什麼也不做。

第二個問題是擔心沒有正確分開。

這裏是我會如何執行此程序:

struct line_data 
{ 
    Time t; 
    double x; 
}; 

// This handles reading a single Time value. 
std::istream & operator >> (std::istream & is, Time & t) 
{ 
    int hh, mm; 
    if (is >> hh >> mm) 
    { 
    // Not happy with the following two lines, too Java-like. :-(
    t.setTimeHours(hh); 
    t.setTimeMinutes(mm); 
    } 
    return is; 
} 

// This handles reading a single line of data. 
std::istream & operator >> (std::istream & is, line_data & ld) 
{ 
    std::string s; 
    if (std::getline(is, s)) 
    { 
    std::istringstream iss(s); 
    // Ensure errors are propagated from iss to is. 
    if (!(iss >> ld.t >> ld.x)) 
     is.setstate(std::ios::failbit); 
    } 
    return is; 
}; 

// This handles processing a single line of data. 
struct line_manip // satisfies concept OutputIterator<line_data> 
{ 
    std::back_insert_iterator<std::vector<Time>> ti; 
    std::back_insert_iterator<std::vector<double>> xi; 

    line_manip(std::vector<Time> & ts, std::vector<double> & xs) 
    : ti(std::back_inserter(ts)) 
    , xi(std::back_inserter(xs)) 
    { 
    } 

    line_manip & operator = (const line_data & ld) 
    { 
    ti = ld.t; 
    xi = ld.x; 
    return *this; 
    } 

    line_manip & operator * () { return *this; } 
    line_manip & operator ++() { return *this; } 
    line_manip & operator ++ (int) { return *this; } 
}; 

int main() 
{ 
    std::ifstream ifs("input.txt"); 
    std::vector<Time> ts; 
    std::vector<double> xs; 
    std::copy(std::istream_iterator<line_data>(ifs), 
      std::istream_iterator<line_data>(), 
      line_manip(ts, xs)); 
    // ... 
} 
相關問題