Getline不从文件中读取或返回
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";
相关文章:
- 从 cin 读取不会返回整个输入字符串
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 读取文件在第二次调用时返回INVALID_HANDLE
- 如何读取指针的值,因为这个指针是函数的返回?
- 如果语句错误地读取字符串,则始终在第一个条件返回
- 无法从文件中读取 - ferror() 返回 1
- 串行读取()不会在没有数据接收的情况下返回值
- Java 套接字读取在第二次读取时返回奇怪字符
- 我想读取文本文件中的所有内容(但文本文件中只有一个双精度值),转换为双精度值并返回值
- 无法从串行端口读取.WaitCommEvent() 永远不会返回
- cudaMemcpy 在从设备读取到主机时返回 cudaErrorInvalidArgument,不清楚原因
- 尝试以随机字符(长读取)打印字符串返回结果
- CreateFile 在尝试打开目录进行读取时始终返回错误 5(访问被拒绝)
- 如何从函数读取返回值
- C++Curl,如何读取返回数据
- 为什么开始读取返回错误ENOMEM(_B)
- ASCII 和二进制 - 按原样显示,但读取返回垃圾
- 在c++中读取返回零
- 从文件中读取返回我没想到的结果,试图理解为什么
- 如何从子进程读取返回代码