繁体   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