C++ C++;:从文件读入数据以填充链表
我试图获取一个.dat文件,该文件有8行,第一行是记录数,其余7行由将填充链接列表的记录的数据组成 我遇到的问题是,我的代码只填充了第一条记录,但使用cout我知道它正在遍历整个文件。下面是我正在使用的代码片段:C++ C++;:从文件读入数据以填充链表,c++,linked-list,C++,Linked List,我试图获取一个.dat文件,该文件有8行,第一行是记录数,其余7行由将填充链接列表的记录的数据组成 我遇到的问题是,我的代码只填充了第一条记录,但使用cout我知道它正在遍历整个文件。下面是我正在使用的代码片段: fstream listBuilder; listBuilder.open("HW06.dat"); if ( listBuilder.is_open() ) { TeleType *current; current = listHead; listBuil
fstream listBuilder;
listBuilder.open("HW06.dat");
if ( listBuilder.is_open() ) {
TeleType *current;
current = listHead;
listBuilder >> numberOfRecords;
while ( listBuilder ) {
string firstName;
string lastName;
string phone;
string name;
listBuilder >> firstName >> lastName >> phone;
name = firstName + " " + lastName;
current->name = name;
current->number = phone;
current = current->nextaddr;
current = new TeleType;
}
listBuilder.close();
} // end if
else {
cout << "***** Error: File failed to open. *****" << endl;
} // end else
fstream列表生成器;
listBuilder.open(“HW06.dat”);
if(listBuilder.is_open()){
电传打字机*电流;
当前=列表头;
listBuilder>>numberOfRecords;
while(列表生成器){
字符串名;
字符串lastName;
字符串电话;
字符串名;
listBuilder>>名字>>名字>>电话;
name=firstName+“”+lastName;
当前->名称=名称;
当前->号码=电话;
当前=当前->下一个数据点;
当前=新电传打字机;
}
close();
}//如果结束,则结束
否则{
cout您应该首先创建下一个节点,然后指向它,而不是相反
current->nextaddr = new TeleType;
current = current->nextaddr;
要使链接列表正常工作,您需要链接节点。看起来您并没有这样做。与仅使用列表相反,在纸上绘制框和箭头是调试带有指针的代码的最佳方法。谢谢,这正是我的问题!