getline函数读取文件中没有的随机字符;两个随机字符与记事本keyword.txt中的数据一起读取

发布于 2024-09-30 01:12:33 字数 648 浏览 1 评论 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;
}
#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剑心龙吟 2024-10-07 01:12:33

您不会检查 getline() 是否确实成功读取了该行。您的循环应该检查 getline() 的返回值:

while(getline(input,t)) {
    L.insert_ordered(t);
    L++;
}

You don't check if getline() did actually successfully read the line. Your loop should check the return value of getline():

while(getline(input,t)) {
    L.insert_ordered(t);
    L++;
}
想挽留 2024-10-07 01:12:33

也许这是 unicode 文件,并且您在文件开头看到字节顺序标志字节

maybe this is unicode file and you are seeing the byte order flag bytes at the beginning of the file

旧人 2024-10-07 01:12:33
void keywords::load()
{    
    string t;    
    while(input)    
    {        
        getline(input,t);//error line        
        L.insert_ordered(t);        
        L++;    
    }    
    L.print(cout);
}

这是关键行:

while(input) 

这实际上可以翻译为:

while(!input.fail()) 

查看 ifstream::fail() 的文档 http://www.cppreference.com/wiki/io/eof。它解释了 ifstream::fail() (和 eof())如何工作。在您的情况下,应在尝试从文件中读取之后、但在尝试使用读取值之前立即检查 input.fail() 。

换句话说,您必须在 getline(..) 之后但在 L.insert_ordered(...) 之前立即检查

尝试这个 -

void keywords::load()
{    
    string t;    
    while(true)    
    {        
        getline(input,t);
        if (!input)
            break;         
        L.insert_ordered(t);        
        L++;    
    }    
    L.print(cout);
}

这是您可以做到的另一种方法:

void keywords::load()
{    
    string t;  

    getline(input,t);  
    while(input)    
    {               
        L.insert_ordered(t);        
        L++;    

        getline(input,t);

    }    
    L.print(cout);
}

我还没有尝试编译和运行这些,所以您可能想自己解决这个问题,但我希望你能了解总体思路。我不确定程序的其余部分是否正常工作(我有点不确定代码中 L++ 的用途),但我希望解释如何使用 ifstream::fail() (或隐式调用它通过直接测试流)有帮助。

void keywords::load()
{    
    string t;    
    while(input)    
    {        
        getline(input,t);//error line        
        L.insert_ordered(t);        
        L++;    
    }    
    L.print(cout);
}

Here is a key line:

while(input) 

This really translates to:

while(!input.fail()) 

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 -

void keywords::load()
{    
    string t;    
    while(true)    
    {        
        getline(input,t);
        if (!input)
            break;         
        L.insert_ordered(t);        
        L++;    
    }    
    L.print(cout);
}

Here is another way you can do it:

void keywords::load()
{    
    string t;  

    getline(input,t);  
    while(input)    
    {               
        L.insert_ordered(t);        
        L++;    

        getline(input,t);

    }    
    L.print(cout);
}

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.

北斗星光 2024-10-07 01:12:33

奇怪的行为。这实际上是编译吗?你能用一些输出来说明吗?我问这个问题的原因是,据我所知,
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 );.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文