getline函数读取文件中没有的随机字符;两个随机字符与记事本keyword.txt中的数据一起读取
#include "keywords.h"
#include <iostream>
#include <fstream>
#include "llist.h"
#include <string>
using namespace std;
//default constructor
List<string> L;
keywords::keywords(){
}
void keywords::open_file(string filename)
{
input.open(filename.c_str());
}
void keywords::close_file(){
input.close();
}
void keywords::load()
{
string t;
while(input)
{
getline(input,t);//error line
L.insert_ordered(t);
L++;
}
L.print(cout);
}
bool keywords::find(std::string token)
{
L.start();
if(L.find(token)==1)
return 1;
else return 0;
}
#include "keywords.h"
#include <iostream>
#include <fstream>
#include "llist.h"
#include <string>
using namespace std;
//default constructor
List<string> L;
keywords::keywords(){
}
void keywords::open_file(string filename)
{
input.open(filename.c_str());
}
void keywords::close_file(){
input.close();
}
void keywords::load()
{
string t;
while(input)
{
getline(input,t);//error line
L.insert_ordered(t);
L++;
}
L.print(cout);
}
bool keywords::find(std::string token)
{
L.start();
if(L.find(token)==1)
return 1;
else return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不会检查
getline()
是否确实成功读取了该行。您的循环应该检查 getline() 的返回值:You don't check if
getline()
did actually successfully read the line. Your loop should check the return value ofgetline()
:也许这是 unicode 文件,并且您在文件开头看到字节顺序标志字节
maybe this is unicode file and you are seeing the byte order flag bytes at the beginning of the file
这是关键行:
这实际上可以翻译为:
查看 ifstream::fail() 的文档 http://www.cppreference.com/wiki/io/eof。它解释了 ifstream::fail() (和 eof())如何工作。在您的情况下,应在尝试从文件中读取之后、但在尝试使用读取值之前立即检查 input.fail() 。
换句话说,您必须在 getline(..) 之后但在 L.insert_ordered(...) 之前立即检查
尝试这个 -
这是您可以做到的另一种方法:
我还没有尝试编译和运行这些,所以您可能想自己解决这个问题,但我希望你能了解总体思路。我不确定程序的其余部分是否正常工作(我有点不确定代码中 L++ 的用途),但我希望解释如何使用 ifstream::fail() (或隐式调用它通过直接测试流)有帮助。
Here is a key line:
This really translates to:
Look at the documentation of ifstream::fail() at http://www.cppreference.com/wiki/io/eof. It explains how ifstream::fail() (and eof()) work. In your case, input.fail() should be checked immediately after attempting to read from the file, but before trying to use the read value.
In other words, you must check immediately after getline(..) but before L.insert_ordered(...)
Try this -
Here is another way you can do it:
I haven't tried to compile and run these, so you might want to work that out yourself, but I hope you get the general idea. I'm not sure if the rest of your program works ok or not (I"m a little unsure about the purpose of L++ in your code), but I hope the explanation about how to use ifstream::fail() (or implicitly calling it by testing the stream directly) helps.
奇怪的行为。这实际上是编译吗?你能用一些输出来说明吗?我问这个问题的原因是,据我所知,
getline
实际上是这样定义的:istream& getline(char*s,streamsizen);
。Strange behaviour. Is this actually compiling? Could you illustrate with some output? The reason i'm asking is because, to my knowledge,
getline
is actually defined this way:istream& getline (char* s, streamsize n );
.