C++ Textfile进入单链表,用户搜索word,C++;

C++ Textfile进入单链表,用户搜索word,C++;,c++,time,full-text-search,user-input,singly-linked-list,C++,Time,Full Text Search,User Input,Singly Linked List,我试图将一个大的文本文件读入一个单链表,将每个单词分开(使用空格作为分隔符),然后让用户查找文本文件中的任何单词。我还试着计算搜索需要多长时间。我可以很好地启动程序,控制台会提示用户输入,但在我输入要搜索的单词后,程序停止工作。知道是什么导致了这个错误吗 主要内容如下: #include <iostream> #include <fstream> #include <time.h> #include <string> #include "List.

我试图将一个大的文本文件读入一个单链表,将每个单词分开(使用空格作为分隔符),然后让用户查找文本文件中的任何单词。我还试着计算搜索需要多长时间。我可以很好地启动程序,控制台会提示用户输入,但在我输入要搜索的单词后,程序停止工作。知道是什么导致了这个错误吗

主要内容如下:

#include <iostream>
#include <fstream>
#include <time.h>
#include <string>
#include "List.h"
using namespace std;

int main(){
    List myList;
    string s;
    string line;
    ifstream ifs("pg1250.txt");

    if(ifs.is_open())
    {
        while(getline(ifs, line, ' '))
        {
            myList.add(line);
        }
        ifs.close();
    }
    string key;
    cout<<"Please enter a word to find"<<endl;
    cin>>key;
    myList.find(key);
    return 0;
}
#包括
#包括
#包括
#包括
#包括“List.h”
使用名称空间std;
int main(){
列出我的清单;
字符串s;
弦线;
ifstream ifs(“pg1250.txt”);
if(if.is_open())
{
while(getline(ifs,line“”)
{
myList.add(行);
}
ifs.close();
}
字符串键;
coutnext=结束;
光标=结束;
}
}
无效列表::已读(列表l){
弦线;
ifstream ifs(“pg1250.txt”);
if(if.is_open())
{
while(getline(ifs,line“”)
{
l、 添加(行);
}
ifs.close();
}
}
无效列表::查找(字符串键)
{
光标=开始;
int位置=1;
while(光标->数据!=键)
{
光标=光标->下一步;
位置++;
如果(游标==结束)
打破
如果(光标->数据==键)
{
库特

您尚未初始化这些变量。类变量的默认值不是NULL,并且在
add()
方法中,您正在检查
if(cursor==NULL)
。这可能是问题所在。

不仅您忘记初始化start和cursor(如HadeS所说),而且您还尝试在构建节点之前取消对start的引用(List.cpp的第10行),这应该会导致segfault。

为什么不尝试解决
stl List
的问题呢?练习的一部分是创建我自己的链表。如果您的目的是读取空格分隔的字符串,只需使用
std::string
提取操作符来
std::istream
(ifs>>str){do something with str}
感谢您捕捉到这一点。我已经解决了这个问题。我认为我没有正确使用ifstream,因为当我在if之后尝试测试cout时,条件中的任何内容都不会打印出来,它会直接跳出循环。有什么想法吗?不确定文件中是否存在文本。相同的代码对我有效。
#include "List.h"
#include <iostream>
using namespace std;

List::List(){
}
void List::add(string s){
    if(cursor==NULL)
    {
        start->data=s;
        cursor = start;
        end = start;
    }
    else
    {
        end = new Node(s);
        cursor->next = end;
        cursor = end;
    }

}
void List::read(List l){
    string line;
    ifstream ifs("pg1250.txt");

    if(ifs.is_open())
    {
        while(getline(ifs, line, ' '))
        {
            l.add(line);
        }
        ifs.close();
    }
}
void List::find(string key)
    {
        cursor = start;
        int position = 1;
        while(cursor->data!=key)
        {
            cursor = cursor->next;
            position++;
                    if(cursor==end)
                    break;


        if(cursor->data==key)
        {
            cout<<"Found at " << position << endl;
        }

        else
        {
            cout<<"Not found!" <<endl;
        }
}
#include <fstream>
#include "Node.h"
using namespace std;
class List{
public:
List();
void add(string);
void find(string);
void read(List);
Node* cursor;
Node* start;
Node* end;
};
#include "Node.h"


Node::Node()
{
    data = "";
    next = NULL;
}

Node::Node(string s)
{
    data = s;
    next = NULL;
}
#include <string>
using namespace std;

class Node

{public:
    Node(void);
    Node(string);
    string data;
    Node* next;
};
List::List(){
     start = NULL;
     cursor = NULL;
}