简体   繁体   English

getline dosnt从文件读取或返回

[英]getline dosnt read or return from file

I am trying to read a string from a file and compare it to another string. 我正在尝试从文件中读取一个字符串,并将其与另一个字符串进行比较。 For some reason getline just returns and empty line. 由于某种原因,getline仅返回而空行。

int count =0;
string line;
ifstream emailFile;
emailFile.open("email.txt");


if (emailFile.is_open())
{
    cout << "file open \n";


while (emailFile.eof()!=true)
{
    getline(emailFile,line);
    cout <<line<<endl;


        for (int i=0; i<27; i++)
        {
            cout <<"line: "<< line << endl;
            cout<< "Spamword: "<< spamWords[i]<<endl;
            if (line.find(spamWords[i]) != string::npos)
            {
                cout <<"found line: "<< line << endl;
                cout<< "found Spamword: "<< spamWords[i]<<endl;
                count +=2;
            }

        }
}
}

else cout<< "file not found";

cout <<count;

}

I have tried troubleshooting it and I really don't see why nothing is being read. 我尝试对其进行故障排除,但我真的不明白为什么什么也没读。

Any help would be appreciated. 任何帮助,将不胜感激。

This person was having the same problem that you are having. 此人遇到的问题与您遇到的问题相同。

http://www.cplusplus.com/forum/beginner/27799/ http://www.cplusplus.com/forum/beginner/27799/

Make sure that your txt file is in the same directory that the exe is built into. 确保您的txt文件位于exe内置目录中。 From the above link this line of code might help ensure you are actually in the file. 通过上面的链接,此行代码可能有助于确保您实际位于文件中。

//Check if data file is open and reports
if(read.is_open())
  cout << "File ./" << file_name << " is open.\n";
  else
cout << "Error opening " << file_name << ".\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM