簡體   English   中英

上線並立即測試EOF

[英]getline and testing EOF at once

我想問一下我試圖閱讀Getline和EOF問題但沒有幫助的問題。

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

如果text.txt文件中沒有文本,則說明已找到內容。 但是我不知道為什么或在哪里犯了錯誤...

我想要的是:搜索字符串,如果txt文件中沒有文本,我希望它顯示EOF或其他內容。 它仍然說-即使文件為空-我正在尋找的字符串也位於第一行第一位置-例如

我把那里的代碼:

#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進行了嘗試,此文件為空。

使用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;
}

嘗試讀取 ,請務必測試輸入是否成功! 流無法知道您要嘗試執行的操作。 它只能報告到目前為止嘗試是否成功。 所以,你會做類似的事情

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

在失敗的情況下,您可能想使用eof()來確定失敗是否是由於到達流的末尾std::ios_base:eofbit到達文件的末尾,因此設置std::ios_base:eofbit通常不是錯誤但只是表明您已經完成。 例如,當已知要讀取多少行但獲得更少的行時,仍然可能是一個錯誤。

您在這里有一個錯誤:

found = what.find(sentence);

您正在尋求內部whatsentence 如果sentence為空,則會找到它。

更改為

found = sentence.find(what);

您應該明確學習如何使用調試器。 這樣,您會很快發現此類問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM