我正在尝试将链接列表写入二进制文件,然后在程序启动时将其读回。
我为此编写了以下代码:
class Node
{
private:
int pos;
int data;
Node* next;
Node* prev;
friend class Linklist;
public:
Node(int d):data(d),pos(-1),next(NULL),prev(NULL)
{}
};
#include <new>
#include <sstream>
#include<iostream>
#include"linklist.h"
bool Linklist::insert(int data, bool updateDisk)
{
if(isExist(data))
{
std::cout<<"Tried to insert duplicate data";
return false;
}
Node *temp = new (std::nothrow) Node(data);
if(temp == NULL)
{
return false;
}
if(tail == NULL)
{
tail = temp;
head = temp;
}
else
{
tail->next = temp;
temp->prev = tail;
tail = temp;
}
if(updateDisk)
{
tail->pos = nextPosition++;
updateAdditionOnDisk(tail);
}
}
bool Linklist::insert(int data, int location)
{
if(isExist(data))
{
return false;
}
Node *temp = new (std::nothrow) Node(data);
if(temp == NULL)
{
return false;
}
Node *it = head;
while(it != NULL)
{
if(it->pos > location)
{
break;
}
it = it->next;
}
if(it)
{
temp->prev = it->prev;
temp->next = it;
it->prev->next = temp;
it->prev = temp;
}
//tail->position = updateAdditionOnDisk(data, nextAvailablePos);
}
bool Linklist::erase(int data)
{
if(tail == NULL)
return false;
Node *temp = head;
while(temp != NULL)
{
if(temp->data == data)
{
//nextAvailablePos = updateDeletionOnDisk(temp->position, nextAvailablePos);
if(temp == head)
{
if(head->next)
{
head = head->next;
head->prev = NULL;
delete temp;
}
else
{
delete head;
head = NULL;
tail = NULL;
}
return true;
}
else if(temp == tail)
{
if(head == tail)
{
delete head;
head = NULL;
tail = NULL;
}
else
{
tail = tail->prev;
tail->next = NULL;
delete temp;
}
return true;
}
else
{
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
return true;
}
}
temp = temp->next;
}
return false;
}
bool Linklist::isExist(int data)
{
Node *temp = head;
while(temp != NULL)
{
if(temp->data == data)
{
return true;
}
temp = temp->next;
}
return false;
}
void Linklist::display( )
{
Node *temp = head;
while(temp != NULL)
{
std::cout<<temp->data;
if(temp->next)
{
std::cout<<"-->";
}
temp = temp->next;
}
}
int Linklist::updateAdditionOnDisk(Node *node)
{
oFile.seekp (0, std::ios::beg);
oFile.write( (char*)&nextPosition, sizeof(int) );
oFile.flush();
int count = 0,pos = 0;
bool inserted = false;
Node n(-1);
iFile.seekg (0, std::ios::beg);
iFile.read((char*)&pos, sizeof(int));
while(!iFile.eof())
{
std::cout<<"iFile is good";
if(n.pos == -1)
{
oFile.seekp(sizeof(int) + (sizeof(Node) * count) , std::ios::beg);
oFile.write( (char*)node, sizeof(Node) );
oFile.flush();
inserted = true;
break;
}
count++;
}
if(!inserted)
{
oFile.seekp(sizeof(int), std::ios::beg);
oFile.write( (char*)node, sizeof(Node) );
oFile.flush();
}
}
int Linklist::updateDeletionOnDisk(int data)
{
int temp = nextPosition + 1;
oFile.seekp (0, std::ios::beg);
oFile.write( (char*)&nextPosition, sizeof(int) );
oFile.flush();
int count = 0,pos = 0;
bool inserted = false;
Node n(-1);
n.pos = -1;
iFile.seekg (0, std::ios::beg);
iFile.read((char*)&pos, sizeof(int));
while(!iFile.eof())
{
std::cout<<"iFile is good";
iFile.read((char*)&n, sizeof(int));
if(n.data == data)
{
n.pos = -1;
oFile.seekp(sizeof(int) + (sizeof(Node) * count) , std::ios::beg);
oFile.write( (char*)&n, sizeof(Node) );
oFile.flush();
break;
}
count++;
}
}
void Linklist::createListFromFile ()
{
Node n(-1);
iFile.seekg(0, std::ios::beg);
if(!iFile.eof())
{
iFile.read((char*)&nextPosition, sizeof(int));
while(!iFile.eof())
{
iFile.read((char*)&n, sizeof(Node));
}
}
}
Linklist::~Linklist()
{
while(head)
{
Node * temp = head;
head = head->next;
delete temp;
}
}
int main ()
{
char choice;
int data;
Linklist l;
while (1)
{
std::cout << "\n\nSelect Opration to performed on LinkList"<<std::endl;
std::cout << "1 Insert "<<std::endl;
std::cout << "2 Delete "<<std::endl;
std::cout << "3 IsExist "<<std::endl;
std::cout << "4 Display "<<std::endl;
}
}
Linklist::~Linklist()
{
while(head)
{
Node * temp = head;
head = head->next;
delete temp;
}
}
但是代码给我的是垃圾输出。
谁能指出我在代码中可能存在的错误。
谢谢
最佳答案
在函数 Linklist::updateAdditionOnDisk()
中,局部变量 n
的作用是什么?你初始化它然后检查它但永远不要改变它的值。也许您应该消除它并只查看 node
? (顺便说一下,它应该声明为指向 const Node
的指针,这样您就不会在保存时错误地修改 Node
实例)。
我越看 Linklist::updateAdditionOnDisk()
就越困惑。 pos
是局部变量,每次调用函数时都会从输出文件中读取;它从未被使用或修改过。你有一个循环,它将永远循环直到 n.pos
的值不是 -1
;它还会检查 iFile.eof()
但循环不会从 iFile
中读取,因此检查将始终成功或始终失败。 count
是一个局部变量,总是从 0 开始......这实际上可能是你的问题。考虑这一行:
oFile.seekp(sizeof(int) + (sizeof(Node) * count) , std::ios::beg);
这使用 count
在输出文件中查找。但是 count
将始终为 0,因此这将继续寻找文件的开头并覆盖之前的记录。也许您在这里想要 pos
而不是 count
?或者 node.pos
?
您是否尝试过在调试器中单步执行您的程序并观察它的作用?就此而言,让它在每次传递时打印变量值,如 count
并观察它如何在输出文件中查找。
而且,如果您只是想解决存储值的问题,您可能希望只存储整数值(以计数为前缀)或使用已编写、已调试的库来存储您的数据你(JSON 格式,或 HDF5 格式,甚至是 SQLite 数据库文件)。
Linklist::insert()
可能会失败。如果 location
对于列表中的任何 Node
实例来说太大,则 while
循环将运行到最后,留下 it
设置为 NULL
然后什么都不会插入。此外,您在任何地方都没有 return true
来表示添加成功。
Linklist::createListFromFile()
从不调用 insert()
。所以它实际上并没有建立链表。我认为您之前发布的代码正在调用 insert()
但这段代码并未调用它。
抱歉,我没有更多时间来做这个了。以下是一些指南类型的建议:
将链表写入磁盘的函数根本不需要查找。只写数据记录数,后面是所有记录。
从磁盘读取链表的函数根本不需要查找。只需读取数据记录的数量,然后循环直到从磁盘读取那么多记录,对从磁盘读取的每个值调用
insert()
。您经常调用
flush()
,但您根本不需要这样做。只需写入所有记录,然后关闭输出文件即可。写入磁盘的函数应该使用
const
指针。将数据写入磁盘不应更改数据,因此请使用const
声明您的意图。编写可能可行的最简单代码,然后对其进行测试并确保它按照您希望的方式运行。然后,向其添加更多功能。我建议您编写一个测试程序,生成一个包含值 1、2、3、4、5 的链表,并单步观察它将该列表保存到磁盘,然后读取它并从磁盘文件构建一个新的链表.
祝你好运,玩得开心。
关于c++ - 在 C++ 中读取和写入同一个二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18994279/