【发布时间】:2017-10-25 03:48:15
【问题描述】:
我正在尝试读取文件并使用双向链表来存储数据并输出存储的数据。但是每当我运行我的程序时,它什么都不输出并终止。我编译代码没有错误。
带有预处理器指令和结构的头文件
#include <iostream>
#include <fstream>
using namespace std;
typedef struct sentry sentry;
struct stud
{
string term;
string title;
string description;
string tktNum;
string location;
string lecDay;
string instructor;
string labLoc;
string labDay;
string labInstruct;
string units;
string preReqs;
string grade;
};
struct slist
{
int length;
sentry *first;
sentry *last;
};
struct sentry
{
slist *list;
sentry *next;
sentry *prev;
stud *data;
};
void readFile(slist *&header);
我的 main.cpp 它调用读取文件函数并输出它
#include "header.h"
int main()
{
slist *header = NULL;
sentry *temp, *node;
temp = header->first;
readFile(header);
for(int i=0; i<header->length; ++i)
{
cout << node->data->term << endl;
cout << node->data->title << endl;
cout << node->data->description << endl;
cout << node->data->tktNum << endl;
cout << node->data->location << endl;
cout << node->data->lecDay << endl;
cout << node->data->instructor << endl;
cout << node->data->labLoc << endl;
cout << node->data->labInstruct << endl;
cout << node->data->units << endl;
cout << node->data->preReqs << endl;
cout << node->data->grade << endl;
node-> prev = header-> last;
node-> next = NULL;
temp = header -> last;
temp-> next = node;
header-> last = node;
node = temp->prev;
}
return 0;
}
我的 readFile 函数 - 它从文本文件中读取数据并将数据存储到链表中
#include "header.h"
void readFile(slist *&header)
{
ifstream fin;
sentry *node, *temp;
fin.open("data.txt");
while(!fin.eof())
{
if(header == NULL)
{
header = new slist;
header-> length = 0;
header-> first = NULL;
header-> last = NULL;
node = new sentry;
header-> first = node;
header-> last = node;
node-> prev = NULL;
node-> next = NULL;
}else
{
node = new sentry;
node-> prev = header-> last;
node-> next = NULL;
temp = header -> last;
temp-> next = node;
header-> last = node;
}
node->data = new stud;
getline(fin, node->data->term);
getline(fin, node->data->title);
getline(fin, node->data->description);
getline(fin, node->data->tktNum);
getline(fin, node->data->location);
getline(fin, node->data->lecDay);
getline(fin, node->data->instructor);
getline(fin, node->data->labLoc);
getline(fin, node->data->labDay);
getline(fin, node->data->labInstruct);
getline(fin, node->data->units);
getline(fin, node->data->preReqs);
getline(fin, node->data->grade);
header->length++;
}
}
我的 data.txt 文件(用于从中读取数据的文本文件)
Fall 2222
CS101
Computer Science Intro
12345
SCI546
MWF 1230PM
John Doe
SCI547
MWF 230PM
John Doe
4
N/A
B
Spring 111
English 101
Intro to English
6789
LI123
TTH 130PM
Jane Doe
N/A
N/A
N/A
N/A
3
N/A
A
【问题讨论】:
标签: c++ eclipse pointers linked-list doubly-linked-list