如何从文件中检索链接列表(在Turbo C+;+;) 我已经在我的学校CS项目上工作了几个星期,我用C++编写了一个基于文本的程序来管理超市/商店。当我试图存储数据库并从文件存储中读取时,遇到了一些问题

如何从文件中检索链接列表(在Turbo C+;+;) 我已经在我的学校CS项目上工作了几个星期,我用C++编写了一个基于文本的程序来管理超市/商店。当我试图存储数据库并从文件存储中读取时,遇到了一些问题,c++,turbo-c++,C++,Turbo C++,完整来源 我知道TurboC++是一个非常过时的编译器,但它是我规定的教学大纲的一部分,所以没有办法。(谢天谢地,下一批将学习python) 我的主要概念是使用自引用结构作为由类及其函数处理的单链表的节点 struct product //Self referencial structure to store the product details { unsigned int code; char name[100]; char category[40]; u

完整来源

我知道TurboC++是一个非常过时的编译器,但它是我规定的教学大纲的一部分,所以没有办法。(谢天谢地,下一批将学习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;
};
store
cart
是类
list
的全局声明对象,用于存储单独的链表

我主要为程序选择了链表格式,并决定添加保存到文件和从文件加载,直到后来我发现文件处理是项目的必修部分。这是我为同样的目的编写的代码-

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();
}
这是我为同样的目的编写的代码- 我还添加了一些测试代码作为switchcase选项,在这里我只需从文件中读取而无需链接,并且它也不会显示所需的输出

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;
案例7:
clrsc();
ifstream-fin(“DATABASE.DAT”,ios::in);
产品温度;
fin.seekg(0,ios::beg);
而(fin.read((字符*)和temp,sizeof(产品)))
{
//财务读数((字符*)和温度,尺寸(产品));

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

我所做的是将
product
更改为一个类,继承自基类
prod
,其中
prod
包含与结构相同的内容,但指向下一个的指针除外。然后我编写了一个函数
Delink()
将链接列表中的每个
产品
转换为
产品
,并将其写入文件。
然后,在读取文件时,我将每个<代码> PROD/CUT>转换成<代码>产品<代码>,并将其链接回构建链表。这似乎已经解决了我的问题。

查看。即使C问题,问题与C++流相同,实际上也一样。当重新启动程序时,内存插件。将不同。您不能将
product*next;
保存到文件中,然后再加载它以获取有效地址。您需要不同的方式“链接”fileOff主题中的节点:您应该习惯于实现构造函数的初始化器列表(不要与
std::initializer\u list
混淆):
list():head(nullptr),tail(nullptr){}
,特别是当涉及复杂对象时,您更喜欢按值直接初始化,而不是随后的默认初始化和赋值(后者通常成本更高)。更重要的是:某些类型(引用、非默认可构造的、常量成员)只有这样才能初始化。也许这里的问题:<代码>得到(TEMP-> Name);——不应该使用<代码>获取< /代码>,没有办法阻止缓冲区溢出(实际上,该函数甚至在以后的标准中都被从C和C++中删除了,因为它不安全)!可能您输入的产品名称太长?然后终止的空字符被写入缓冲区之外(实际上是未定义的行为!)并丢失。如果没有,则(纯属偶然!!!)任何包含在结构中的任意null字节,都会超出分配内存,并且很可能会发生崩溃。Turbo C++不支持命名空间或C++标准模板库,所以<代码> STD::列表< /COD>,<代码> STD::向量< /代码>,甚至<代码> STD::String < /Cord>不能使用。它是一个非常好的ANSI编译器。恼人的,它就像C和C++之间的一半(所以,C+?)
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;