Getline不从文件中读取或返回

getline dosnt read or return from file

本文关键字:读取 返回 文件 Getline      更新时间:2023-10-16

我正试图从文件中读取字符串并将其与另一个字符串进行比较。由于某些原因,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;
}

我已经尝试了故障排除,我真的不明白为什么没有读取。

任何帮助都会很感激。

这个人和你有同样的问题。

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

确保你的txt文件与exe文件所在的目录相同。从上面的链接,这行代码可以帮助确保您实际上在文件中。

//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";