[英]Reading a text file to a doubly linked list and outputting it - C++
我正在嘗試讀取文件,並使用雙向鏈表來存儲數據並輸出所存儲的數據。 但是,每當我運行程序時,它都不會輸出任何內容並終止。 我在編譯代碼時沒有錯誤。
帶有預處理器指令和結構的頭文件
#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
slist *header = NULL;
您永遠不會為header
分配任何內容。 header
始終為NULL
。 另外,最好將其稱為list
而不是header
。 您需要:
slist list;
您還有其他未初始化的指針,例如sentry *node;
除非它指向某物,否則不能使用它。
您在某些部分使用了C風格的聲明。 C型結構可以如下:
typedef struct sentry_t sentry; //in C
在C ++中,您可以簡單地編寫struct sentry;
但是您實際上並不需要這個。 您需要以下結構:
struct stud
{
... as before
};
struct snode
{
stud data;
snode *next;
snode *prev;
};
struct slist
{
int length;
snode *head;
snode *tail;
};
slist
是雙鏈表,而snode
是其節點。
請注意,我更改了結構,現在stud
包含在snode
,因此您無需使用new
進行單獨的分配(在實際應用中,您必須釋放所有內存,以避免不必要的分配)
現在,您可以聲明並初始化列表:
int main()
{
//declare and initialize the list
slist list;
list.head = NULL;
list.tail = NULL;
list.length = 0;
readFile(list);
snode *node = list.head;
for(int i = 0; i < list.length; i++)
{
cout << node->data.term << endl;
cout << node->data.title << endl;
cout << node->data.description << endl;
...
node = node->next;
cout << "\n";
}
return 0;
}
讀取功能應如下所示:
void readFile(slist &list)
{
ifstream fin("file");
while(fin.good())
{
snode *node = new snode;
if(list.head == NULL)
{
node->prev = NULL;
node->next = NULL;
list.head = node;
list.tail = node;
}
else
{
list.tail->next = node;
node->prev = list.head;
node->next = NULL;
list.tail = node;
}
getline(fin, node->data.term);
getline(fin, node->data.title);
getline(fin, node->data.description);
...
getline(fin, node->data.grade);
list.length++;
string blankline;
if(!getline(fin, blankline))
break;
}
}
使用fin.good()
代替!fin.eof()
。 請注意您的文件中有一個空行。 您想跳過該循環或在到達最后一行時中斷循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.