我试图从文件中读取链表。但是由于某些原因,它甚至没有读取文件的内容。
这是文件的结构:
Product 1
Category
22.33
Product 2
Category
44.23
Product 3
Category
66.55
,这是读取文件内容的函数。它调用addproduct函数来按排序顺序添加它所读取的项。
void load (NodePtr head)
{
// create a variable to attach to the file
ifstream input;
input.open("data.txt");
// check to see if the file opened correctly, and if not, exit program
if (input.fail())
{
cout << "n Data file not found n";
return;
}
ListItemType data;
while((! input.eof()) && (head != NULL) )
{
input >> data.productname;
input >> data.category;
input >> data.productprice;
addproduct(head, data);
}
需要接收head的新指针。不是返回void,而是返回NodePtr.
NodePtr load( NodePtr head ){
...
return head;
}