2017-08-12 32 views
-4

我在Java中經歷了十年之後迴歸C++,現在我已經堅持了兩個星期了,我看到很多有getline問題的帖子,這些讓我添加了很多「cout」試圖找到確切的錯誤,我已經發現了錯誤,失敗和eof位,但仍然存在相同的問題.getline不填充字符串或進入while循環。 不知道如果它很重要,但我在NetBeans中編寫Win7 PC和編譯/運行在rasberry pi(運行raspbian(Debian,我認爲))C++ getline失敗,並顯示「Success」:-(

我想要做的就是讀取使用getline的簡單文本文件!應該是基本的('scuse the pun :-)) 我試圖用Unix和ASCII行結尾(0x0A和0x0D0A)創建文本文件 - mad沒有區別。我使用創建文本文件的相同用戶標識運行程序,因此沒有權限問題。

Getline設置失敗位(僅...不是eof),但是perror報告「成功」,並且在「工作」中我沒有得到任何數據。

下面是導致錯誤的大部分類 - 帶有許多調試註釋。 「theFile」只是一個fstream('未來版本中的cos可能想將其打開以用於輸出)。但是,在這裏,你可以從輸出中看到,我絕對使用ios :: in打開文件。 「問題」發生在getValue成員函數中的getline循環(從不輸入)。 我沒有包含構造函數,但它只設置了一些標誌爲false。而調用者(MJAppl)只是調用MJConfigFile :: getValue(「log_dir」,false)。

string MJConfigFile::getValue (string in_key, bool in_restart) { 
    bool found = false ; 
    string work , ret ; 
    int key_len = in_key.length() , loop_count = 0 ; 
    //char workbuf [ 200 ] ; 

    cout << "MJConfigFile::getValue entry for key " << in_key << " \n" ; 
    cout << "File name is " << getFName() << "\n" ; 
    if (in_restart || !openForInput) { 
     theFile . close() ; 
     open (false) ;   
    } 
    if (theFile . is_open()) { 
     cout << "File " << fullFileName << " is open." << "\n"; 
    } else { 
     perror ("Error while opening file ") ; 
    } 
    while (getline (theFile, work)) { 
     if (loop_count++ > 5) return "Stopped \n" ; 
     cout << "Line '" << work << "'\n"; 
     if (in_key.compare (work) == 0) { 
      ret = work.substr (key_len + 1) ; 
      break ; 
     } 
    } 
    perror ("Error reading file. ") ; 
    cout << "Line after loop '" << work << "'\n"; 
    if (theFile.bad() | theFile.fail() | theFile.eof()) { 
     if (theFile . bad()) cout << "Bad file." << endl ; 
     if (theFile . fail()) cout << "Fail file." << endl ; 
     if (theFile . eof()) cout << "eof file." << endl ; 

    } 
    return ret ; 
} 

void MJConfigFile::open() { 
    this -> open (false) ; 
} 
void MJConfigFile::open (bool in_for_output) {  

    if (in_for_output) { 
     theFile . open (fullFileName.c_str(), ios::out) ; 
     if (theFile . is_open()) { 
      openForOutput = true ; 
      cout << "File is open for output. \n" ; 
     } 
    } else { 
     theFile . open (fullFileName.c_str(), ios::in) ; 
     if (theFile . is_open()) { 
      openForInput = true ; 
      cout << "File is open for input. \n" ; 
     } 
    }  
    if (openForInput || openForOutput) { 
     exists = true ; 
    } 
} 

下面是一個運行示例輸出:

In default MJConfigFile constructor 
    File is open for input. 
    MJAppl.readConfigFile() entry. 
    File is open for input. 
    MJConfigFile::getValue entry for key log_dir 
    File name is /mjmaint/MJApplCfg/MJApplCfg.cfg 
    File /mjmaint/MJApplCfg/MJApplCfg.cfg is open. 
    Error reading file. : Success 
    Line after loop '' 
    Fail file. 
    logdir is 
    0 

這裏是一個示例輸入文件:

Aaaaaa 
    Bbbbbb 
    Cccccc 

,我只是準備放棄並返回到Java所以任何幫助將真正感激。

+6

您的問題未能滿足[mcve]的要求。您正在尋找的幫助是創建[mcve]以包含在您的問題中的說明。 –

+0

看起來過於複雜,與語言風格相反。你是否打算首先查找一個例子來熟悉「C++中的事情是如何完成的」? –

+0

可能重複[逐行讀取文件](https://stackoverflow.com/questions/7868936/read-file-line-by-line) –

回答

0

爲了防止任何人再次遇到同樣特殊的一組失敗/成功條件,問題是我在同一個fstream上打開了兩次。打開似乎成功,但getline不會運行,因爲流的失敗位已設置。謝謝伊戈爾。

相關問題