将文本文件读入链表

Reading text file into linked list

本文关键字:链表 文件 文本      更新时间:2023-10-16

我正在开发一个程序,该程序需要我读取文本文件并将数据插入链表中。我很难将数据读入代码编译的链表中,但输出中没有显示任何内容。任何帮助或建议将不胜感激。

文本文件格式如下:字符串 int

int int int int int

A 83 140 228 286 426 612 486 577 836 0 0 Aaliyah

0 0 0 0 0 0 0 0 0 0 380 215

亚伦 193 208 218 274 279 232 132 36 32 31 41

修道院 0 0 0 0 0 0

0 0 537 451 428

对内部数据结构使用链表。

linked_list<NameSurferEntry> database;

这是我用来解析文件中每一行的函数。

NameSurferEntry::NameSurferEntry(string line){
    vector<string> filelines;
    stringstream inputstringstream;
    year.push_back(1900);
    year.push_back(1910);
    year.push_back(1920);
    year.push_back(1930);
    year.push_back(1940);
    year.push_back(1950);
    year.push_back(1960);
    year.push_back(1970);
    year.push_back(1980);
    year.push_back(1990);
    year.push_back(2000);
    year.push_back(2010);
    while(getline(inputstringstream, line,' ')){
        filelines.push_back(line);
    }
    for(unsigned int i=1; i < filelines.size(); i++){
        cout << year.at(i-1) << " " << filelines.at(0 + 1) << "n";
    }
        cout << " " << endl;
}
string NameSurferEntry::getName(){
    return name;
}
int NameSurferEntry::getRank(int decade){
    return year.at(decade);
}

这是我用来读取文本文件的函数。

NameSurferDataBase::NameSurferDataBase(string filename){
     ifstream input;
    input.open(filename);
    if(!input.is_open()){
        cout << "Not Open";
    }else{
        string temp;
        while(!input.eof()){
            getline(input,temp);
            if(!input.eof()){
                NameSurferEntry entry(temp);
                database.InsertInOrder(entry);
            }
        }
    }
}

按顺序排列:

template <class NODETYPE>
bool linked_list<NODETYPE>::InsertInOrder(NODETYPE value)
{
        if (IsEmpty()) {
                // A variable used to point to data that is being inserted.
                ListNode<NODETYPE> *temp = new ListNode<NODETYPE>;
                if (temp == NULL) return false;
                temp->data = value;
                first = last = current = temp;
                last->next = NULL;
        }
        else if (value >= last->data) {
                return InsertRear(value);
        }
        else if (first->data >= value) {
                return InsertFront(value);
        }
        else {
                // A variable used to point to data that is being inserted.
                ListNode<NODETYPE> *temp = new ListNode<NODETYPE>;
                if (temp == NULL) return false;
                temp->data = value;
                current = first;
                while (current->next->data < temp->data) {
                        current = current->next;
                }
                temp->next = current->next;
                current->next = temp;
                return true;
        }
    return false;
}

查看您提供的以下代码:

在 NameSurferDatabase 中:

       while(!input.eof()){
            getline(input,temp);
            if(!input.eof()){
                NameSurferEntry entry(temp);
                database.InsertInOrder(entry);
            }

您正在正确读取文件中的 1 行并将其放入名为"temp"的字符串中。然后,您将"temp"字符串传递给NameSurferEntry。您现在希望此方法剖析"临时"字符串。

但是,在 NameSurferEntry 中,您键入了以下内容:

    stringstream inputstringstream;
    while(getline(inputstringstream, line,' ')){
    filelines.push_back(line);
    }

您正在声明一个字符串流,默认情况下该字符串流将为空。然后你做了一个while循环来做getline(inputstringstream,line,''(,它将从inputstringstream中获取1行并将其放入变量"line"。这会将变量"line"的内容替换为任何内容。

以下是解决方法:

    stringstream inputstringstream;
    inputstringstream >> line;
    string temp;
    while(inputstringstream << temp){
    filelines.push_back(temp);
    }