0
i am facing a problem in file reading for multiple linked lists written in one file! please help !in c++
i am writing multiple linkedlists in a text file where my linked list contain nodes whose data is of integer type and a count which shows number of nodes in my linked list for a single linked list my read from file and write to file is working perfectly fine but for multiple nodes it causes a runtime error i don't know much but i think file pointer is not maintaining its place from where it has to read 2nd linked list ! please help !
8 Antworten
+ 3
Apologies, I was busy so couldn't respond to my earlier comments! Hope other people's answers helped ☺
+ 1
You probably need to display some of the code, the file format you're using, and the runtime error you're experiencing.
0
void CList::ReadFromFile(ifstream &infile)
{
Node* bp;
Node* rp;
int newCount;
infile.read((char*) &newCount, sizeof(newCount));
if(this->count >= newCount)
{
rp = this->head;
for(int i = 0; i < newCount; i++)
{
rp->ReadFromFile(infile);
rp->print();
bp = rp;
rp = rp->next;
}
bp->next = NULL;
this->count = newCount;
while(rp)
{
bp = rp;
delete bp;
rp = rp->next;
}
}
else
{
rp = head;
for(int i = 0; i < this->count; i++)
{
rp->ReadFromFile(infile);
bp = rp;
rp = rp->next;
}
for(int k = 0; k < (newCount - this->count); k++)
{
rp = new Node();
rp->ReadFromFile(infile);
rp->print();
bp->next = rp;
bp = rp;
}
bp->next = NULL;
this->count = newCount;
}
}
this is my read from file and write to file of list class
0
void Node::ReadFromFile(ifstream &infile)
{
infile.read((char*) &this->data, sizeof(this->data));
}
void Node::WriteToFile(ofstream &ofile)
{
ofile.write((char*) &this->data, sizeof(this->data));
}
this my readfromfile and writetofile of node class
0
int main()
{
ofstream ofile;
ofile.open("ofile.txt");
Node* ptr1 = new Node(1);
Node* ptr2 = new Node(2);
Node* ptr3 = new Node(3);
Node* ptr4 = new Node(4);
Node* ptr5 = new Node(5);
Node* ptr6 = new Node(6);
Node* ptr7 = new Node(7);
Node* ptr8 = new Node(8);
CList L1;
L1.Insert(ptr1);
L1.Insert(ptr2);
L1.Insert(ptr3);
L1.Insert(ptr4);
L1.Insert(ptr5);
L1.Insert(ptr6);
L1.Insert(ptr7);
L1.Insert(ptr8);
CList L3(L1);
L3.Print();
cout<< endl;
L1.Remove();
L1.Remove();
L1.Print();
L3.WriteToFile(ofile);
L1.WriteToFile(ofile);
ofile.close();
ifstream infile;
infile.open("ofile.txt");
CList L2;
L2.ReadFromFile(infile);
L2.Print();
infile.close();
infile.open("ofile.txt");
L2.ReadFromFile(infile);
L2.Print();
return 0;
}
this is my main program
0
post a minimal complete example , if you want me to check it.
0
i am not getting it Jpm7
0
thank you so much everyone ☺