gpt4 book ai didi

c++ - 如何从文件中检索链接列表(在Turbo C++中)

转载 作者:行者123 更新时间:2023-12-02 10:22:56 24 4
gpt4 key购买 nike

我已经在学校的CS项目上工作了几周,并且编写了一个基于文本的程序来管理C++中的超市。当我尝试存储数据库并从文件存储中读取数据库时遇到了一些麻烦。

完整来源here

我知道TurboC++是一个非常过时的编译器,但这是我所规定的类(class)提纲的一部分,因此没有办法。 (非常感谢,下一批将学习python)

我的主要概念是使用自引用结构作为由类及其功能处理的单链接列表的节点。

struct product  //Self referencial structure to store the product details
{
unsigned int code;
char name[100];
char category[40];
unsigned int quantity;
double price;
product *next;
};
class list  //Class to handle the linked list of products
{
private:
product *head, *tail;
public:
list()
{
head = NULL; //By default the list will be empty
tail = NULL;
}
void DelList();
void AppProduct(product *, unsigned int);
void AddProduct(unsigned int);
product* FindProduct(unsigned int);
double CalcTotal();
void ListProducts();
void ModProduct(product *);
void SaveToFile();
void LoadFromFile();
product* PointToNewProduct(product);
product* GetProductFromFile(ifstream &);
product* GetHead()
{
return head;
}
void Clear();
};
storecartlist类的全局声明对象,用于存储单独的链接列表。

我主要为程序选择了链表格式,并决定仅在以后发现文件处理是项目的必修部分时才添加文件保存和从文件加载。这是我为相同代码编写的代码-
void list::SaveToFile()     //self explanatory
{
ofstream fout("DATABASE.DAT", ios::trunc);
product * cur = head;
product temp;
while( cur != NULL )
{
temp = *cur;
fout.write( (char *)&temp, sizeof(product) );
cur = cur->next;
}
fout.close();
}
product * list::PointToNewProduct(product temp) //copy the temp product details into a new pointer and return the pointer
{
product * cur = new product;
cur->code = temp.code;
strcpy(cur->name, temp.name);
strcpy(cur->category, temp.category);
cur->price = temp.price;
cur->quantity = temp.quantity;
cur->next = NULL;
return cur;
}

product * list::GetProductFromFile(ifstream& fin) //retrieve a single product from the given file
{
if( fin.eof() )
return NULL;
product temp;
fin.read( (char *)&temp, sizeof(product) );
cout<<temp.name;
return PointToNewProduct(temp);
}
void list::LoadFromFile() //Function to load struct from file and rebuild the linked list (only returning one item right now)
This is the code I wrote for the same - //Update: I thought I fixed it, but its up to two items now, still not all
{
ifstream fin("DATABASE.DAT", ios::in); //Initialize the stream
head = GetProductFromFile(fin); //initialize head pointer
product * cur = head;
product * nextptr;
do {
nextptr = GetProductFromFile(fin); //get the next product in file
cur = cur->next = nextptr; //link product with the previous product
tail = cur; //make the current pointer the last one
} while( !fin.eof() );
tail->next = NULL;
fin.close();
}

现在的问题是,尽管我能够正确地将链接列表项写入文件,但是尝试读取它会导致它仅检索2个节点,而不管我已写入文件的节点数如何。

与老师进行的一次调试 session 使我相信,这是因为从文件加载一个链接列表时,我并没有删除它,并且在写入文件时仍存在未释放内存的问题。我为释放内存而编写的析构函数从未被调用过,因为我的对象是全局声明的,这导致我编写 DelList();函数并在从文件读取之前对其进行调用。不幸的是,这并没有解决问题。
void list::DelList()
{
product * cur = head, * temp;
while(cur != NULL)
{
temp = cur;
cur = cur->next;
delete temp;
}
delete cur;
delete head;
delete tail;
head = NULL;
tail = NULL;
}


这是我为相同代码编写的代码-
我还添加了一些测试代码作为切换案例的选择,在这里我只是从文件中读取而不进行链接,并且它也不显示所需的输出。
case 7:
clrscr();
ifstream fin("DATABASE.DAT", ios::in);
product temp;
fin.seekg(0, ios::beg);
while( fin.read( (char *)&temp, sizeof(product) ) )
{
//fin.read( (char *)&temp, sizeof(product) );
cout<<temp.name<<'\t'<<temp.category<<'\n';
}
getch();
break;

我的代码有什么问题,如何解决?

最佳答案

我设法通过一种解决方法解决了我的问题,尽管我仍然不知道为什么我的原始代码不起作用,尽管看似问题在于读写文件的指针。

解决该问题的方法是将product更改为一个从基类prod继承的类,其中prod包含与结构相同的东西,但指向下一个的指针除外。然后,我编写了一个函数Delink(),将链接列表中的每个product转换为prod并将其写入文件。
然后,在读取文件时,我将读取的每个prod转换为product,并将其链接回以再次构造链接列表。这似乎已经解决了我的问题。

关于c++ - 如何从文件中检索链接列表(在Turbo C++中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59281843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com