2016-07-16 71 views
0

我想問一下我的問題我試圖讀取Getline和EOF問題,但沒有幫助。getline和一次測試EOF

問題是我不知道哪裏可能會出現錯誤: 使用函數(getline或檢查EOF)是否存在一些問題?

如果text.txt文件中沒有文本,則表示發現了某些內容。但我不知道爲什麼或在哪裏我犯了一個錯誤...

我想要的是:搜索字符串,如果txt文件中沒有文本我希望它說EOF或其他東西。它仍然認爲 - 即使文件是空的 - 字符串我一直在尋找一條線被發現,位置的一個 - 例如

我puting有代碼:

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

int openFile(void); 
int closeFile(void); 
int getTime(void); 
int findTime(); 
int findDate(); 
int stringFind(string); 
bool getOneLine(void); 

string what; 
bool ifound = false; 
string foundstring; 
string filename ; 
fstream inputfile; 
string sentence ; 
size_t found ; 
string foundTime ; 
string foundDate ; 
bool timeIsHere = false; 
bool dateIsHere = false; 

int iterTime = 0; 
int iterDate = 0; 
int line = 0; 


int main (void){ 
    sentence.clear(); 

    cout << " Enter the file name:" << endl; 

    openFile(); 


    while (getOneLine() != false) { 
     stringFind("Time"); 
    } 




    cout << "END OF PROGRAM" << endl; 
    system("PAUSE"); 
    ///getTime(); 
    closeFile(); 


    system("PAUSE"); 
} 


int closeFile(void) { 
    inputfile.close(); 
    cout << " File: " << filename << " - was closed..."; 
    return 0; 
} 

int openFile(void) { 

    cout << " Insert file name in program directory or full path to desired file you want to edit:"<<endl; 
    cout << " Do not use path with a space in directory address or filename ! " << endl; 
    cout<<" "; 
    getline(cin, filename); 

    inputfile.open(filename, ios::in); 

    cout <<" file_state: " << inputfile.fail(); 
    if (inputfile.fail() == 1) { 
     cout << " - Cannot open your file" << endl; 
    } 
    else cout << " - File was openned sucesfully"<< endl; 



    return 0; 

} 


int stringFind(string what) { 
    cout << " I am looking for:" << what << endl; 

    found = what.find(sentence); 
    if (found == string::npos) { 
     cout << " I could not find this string " << endl; 

    } 
    else if(found != string::npos){ 
     cout << " substring was found in line: " << line + 1 << " position: " << found + 1 << endl << endl; 
     ifound = true; 
     foundstring = sentence; 

    } 

    return 0; 


} 



bool getOneLine(void) { 
    if (inputfile.eof()) { 
     cout << "END OF FILE" << endl << endl; 
     return false; 
    } 
    else{ 
     getline(inputfile, sentence); 
     cout << "next sentence is: "<< sentence << endl; 
     return true; 
     } 
} 

我是新手,我沒有一個人要問 - 親自。我試圖編輯While循環和IF,以確保我沒有犯一個嚴重的錯誤,但我不知道。

我試過了,例如sample.txt,這個文件是空的。

+0

這可能有用的信息:https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Galik

回答

2

始終測試讀取嘗試後輸入是否成功!流無法知道你正在嘗試做什麼。它只能報告迄今爲止的嘗試是否成功。所以,你會做這樣的事情

if (std::getline(stream, line)) { 
    // deal with the successful case 
} 
else { 
    // deal with the failure case 
} 

在你可能想用用eof()以確定故障是否是由於到達流末尾的失敗案例:在到達文件的末尾,並因此,std::ios_base:eofbit被設置通常不是一個錯誤,而只是表明你已完成。它可能仍然是一個錯誤,例如,當知道要讀取多少行時,但獲得更少的行時。

+0

是的,你說得對。感謝您的通知。我不知道 - 可能我忽略了它有4個或更多標誌getline返回。並且如果「If」它只能繼續良好的bit-0值。 謝謝我感謝您的幫助和時間。 – naschd

0

你有一個錯誤的位置:

found = what.find(sentence); 

您的what內尋求的sentence。如果sentence爲空,則會找到它。

將其更改爲

found = sentence.find(what); 

你應該definitivly學習如何使用調試器。這樣你會發現這樣的問題很快!

+0

是的,謝謝你。我的問題有一個根本原因。你說得對。但我使用的SW對我來說是新的,我將盡快學會。所以再次感謝你;) 希望這個線程將幫助有類似問題的人。 – naschd

2

使用getline()EOF檢查正確的方法是這樣的:

bool getOneLine(void) { 
    if (getline(inputfile, sentence)) { 
     cout << "next sentence is: "<< sentence << endl; 
     return true; 
    } 
    if (inputfile.eof()) 
     cout << "EOF reached" << endl; 
    else 
     cout << "Some IO error" << endl; 
    return false; 
} 
+0

謝謝,我試圖再次找到關於getline的一些信息。我發現它返回標誌位。所以我可以把它放在一些讓我說 - 條件,就像你放。我的錯誤是我在查找時忽略了返回值。我不知道。謝謝。 – naschd