簡體   English   中英

getline dosnt從文件讀取或返回

[英]getline dosnt read or return from file

我正在嘗試從文件中讀取一個字符串,並將其與另一個字符串進行比較。 由於某種原因,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";

暫無
暫無

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

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