2016-07-16 73 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