2015-06-16 67 views
3

我有一個文件看起來像通過文件,直到*串運行*,然後讀取列,直到*沒有小數*

some header 
this is the first block 
         1 2 3 4 
         5 6 7 8 
         9 10 11 12 
this is the second block 
         1 4 7 10 
         2 5 8 11 
         3 6 9 12 
this is the third block 
         1 2 3 4 
         5 6 7 8 
         9 10 11 12 

我想讀這個文件並檢查單詞「第一」,「第二」和「第三」,以便將以下數字塊讀入數組,以便稍後繪製它們。例如,我只想讀第二塊的柱1和2。主要的問題是,我不能完成閱讀,直到第三塊開始。它在秒塊的第一行之後停止讀取。在一個簡單的方法我的代碼看起來是這樣的。:

#include <string> 
#include <fstream> 
#include <istream> 
#include <vector> 

std::string line; 
std::vector<double> vector1; 
std::vector<double> vector2; 
double v1; 
double v2; 
double v3; 
double v4; 

ifstream infile ("myfile.txt"); 
while (std::getline(infile, line)){ 
    if (line.find("second",0) != std::string::npos){ // when "second" is found start to read block. 
    while (line.find_first_of("123456789.") != std::string::npos){ // while the next line is not a number continue reading. THIS DOESN'T WORK ! 
     infile >> v1 >> v2 >> v3 >> v4; 
     vector1.push_back(v1); 
     vector2.push_back(v2); 
     std::getline(infile, line); 
    } 
    } 
} 
infile.close(); 

cout << "Vector1" << " " << "Vector2" << endl; 
for (unsigned int i = 0; i < vector1.size(); i++){ 
    cout << vector1[i] << " " << vector2[i] << endl; 
} 

預期的結果將是:

Vector1 Vector2 
1  4 
2  5 
3  6 

,但我得到:

Vector1 Vector2 
1  4 

回答

1

這應該可以解決雙讀取內部和退出循環,

ifstream infile ("myfile.txt"); 
while (std::getline(infile, line)) { 
    if (line.find("second",0) != std::string::npos) { 
     while (infile >> v1 >> v2 >> v3 >> v4) { 
      vector1.push_back(v1); 
      vector2.push_back(v2); 
      // note getline is removed, else double reads on break. 
     } 
     // this may be needed if you plan on reading anything else. 
     infile.clear(); 
    } 
} 
+0

我讀了清楚的文檔,但是這對它有什麼好處? – uitty400

+0

使用'operator >>'進行輸入時,如果發生故障,則會設置錯誤,並且必須先清除才能再次使用。如果你只加載一個塊,這不是問題,但是如果在同一個循環中你也掃描「第一」和「第三」,你需要在讀取下一個塊之前清除。 –

1

有兩個問題與您的代碼。 第一個是讀取數字後,「讀取光標」保持在行結束之前。然後,當你打電話給getline時,你最終會得到一個空行。將行讀取方法與其他輸入方法混合時,這是一個常見問題。它發生在混合gets和scanf,以及混合流>>操作符和getline時。

第二個是你正在讀整行,然後再從文件中讀取數字。如果您閱讀了一行文字,您必須閱讀您閱讀的那一行中的數字,而不是從文件中獲取新的輸入。所以試試這一個:

std::getline(infile, line); 
while (line.find_first_of("123456789.") != std::string::npos) { 
    std::stringstream stream(line); 
    stream >> v1 >> v2 >> v3 >> v4; 
    vector1.push_back(v1); 
    vector2.push_back(v2); 
    std::getline(infile, line); 
} 
+0

HM當我這樣做,我得到一個無限循環 – uitty400

+0

對此深感抱歉,請閱讀你的代碼太快並沒有得到有另一個錯誤。我更新了我的答案,現在試試:)請記住包括。 –

+0

這一個不會停在第二個區塊之後。但是我想這樣我的想法'while(line.find_first_of(「123456789。」)!= std :: string :: npos)'並不完成這項工作這裏。 – uitty400

1

我很驚訝,你會得到這樣的結果。如果行包含'秒',則不包含任何數字,並且您不能輸入if。

我會改變身體這樣的:

ifstream infile("myfile.txt"); 
while (std::getline(infile, line)){ 
    if (line.find("second", 0) != std::string::npos) 
    { // when "second" is found start to read block. 
     while (infile){ // while the next line is not a number continue reading. THIS DOESN'T WORK ! 

      infile >> v1 >> v2 >> v3 >> v4; 
      if (!infile) 
       break; 
      vector1.push_back(v1); 
      vector2.push_back(v2); 
      std::getline(infile, line); 
     } 
    } 
} 

所以,當你發現「第二個」請繼續閱讀數字。當數字閱讀失敗if (!infile)退出循環

+0

其實我也是。我有一個std :: ignore語句,但之後我意識到我不需要它。該行被自動跳過。你的方法似乎工作。 – uitty400

相關問題