To Read a link list from file, we've adopted the following procedure:
1. Write each node individually to a file in the function of getData().
2. Read Each node from file one by one and insert it in the link list .
//Reading link list from File in C++
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <Windows.h>
using namespace std;
class linkList{
public:
void getData();
void insert(linkList **head, linkList **tail); // insert at tail
void printAll(linkList *head); // Display all records.
private:
char name[100];
int rollNo;
linkList *next;
};
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::getData(){
cout << "Enter Name: "; cin >> name;
cout << "Enter Roll Number: "; cin >> rollNo;
next = NULL;
ofstream os("nodes of my link list.dat", ios::binary | ios::out | ios::app);
os.write( reinterpret_cast <char*> (this), sizeof(*this) );
os.close();
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::insert(linkList **head, linkList **tail){ // insert at tail
if (*head == NULL && *tail == NULL){
*head = (this);
*tail = (this);
}
else{
(*tail)->next = this;
*tail = this;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::printAll(linkList *head){
for (linkList *temp = head; temp!=NULL; temp=temp->next){
cout << "Name: " << temp->name << endl
<< "Roll Number: " << temp->rollNo << endl << endl;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
/////////////////////////////////////////////////// Main() //////////////////////////////////////
int main(){
linkList *pointerOfLinkList = 0, *head = 0, *tail = 0;
ifstream is;
while (true){
cout << "1. Insert Node\n"
<< "2. Print All\n"
<< "0. Press IT to load link list from file.\t";
int opt; cin >> opt;
switch(opt){
case 1:
pointerOfLinkList = new linkList;
pointerOfLinkList->getData();
pointerOfLinkList->insert(&head,&tail);
cout << "Inserted" << endl;
break;
case 2:
pointerOfLinkList->printAll(head);
break;
case 8:
break;
case 9:
break;
case 0:
is.open("nodes of my link list.dat", ios::binary | ios::in);
pointerOfLinkList = new linkList;
is.read(reinterpret_cast<char*>(pointerOfLinkList),sizeof(*pointerOfLinkList));
while (!is.eof()){
pointerOfLinkList->insert(&head,&tail);
pointerOfLinkList = new linkList;
is.read(reinterpret_cast<char*>(pointerOfLinkList),sizeof(*pointerOfLinkList));
}
is.close();
break;
default:
cout << "Wrong Key pressed" << endl;
}
_getch();
system("cls");
}
return 0;
}
1. Write each node individually to a file in the function of getData().
2. Read Each node from file one by one and insert it in the link list .
//Reading link list from File in C++
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <Windows.h>
using namespace std;
class linkList{
public:
void getData();
void insert(linkList **head, linkList **tail); // insert at tail
void printAll(linkList *head); // Display all records.
private:
char name[100];
int rollNo;
linkList *next;
};
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::getData(){
cout << "Enter Name: "; cin >> name;
cout << "Enter Roll Number: "; cin >> rollNo;
next = NULL;
ofstream os("nodes of my link list.dat", ios::binary | ios::out | ios::app);
os.write( reinterpret_cast <char*> (this), sizeof(*this) );
os.close();
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::insert(linkList **head, linkList **tail){ // insert at tail
if (*head == NULL && *tail == NULL){
*head = (this);
*tail = (this);
}
else{
(*tail)->next = this;
*tail = this;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
void linkList::printAll(linkList *head){
for (linkList *temp = head; temp!=NULL; temp=temp->next){
cout << "Name: " << temp->name << endl
<< "Roll Number: " << temp->rollNo << endl << endl;
}
}
//````````````````````````````````````````````````````````````````````````````````````````````````````````````
/////////////////////////////////////////////////// Main() //////////////////////////////////////
int main(){
linkList *pointerOfLinkList = 0, *head = 0, *tail = 0;
ifstream is;
while (true){
cout << "1. Insert Node\n"
<< "2. Print All\n"
<< "0. Press IT to load link list from file.\t";
int opt; cin >> opt;
switch(opt){
case 1:
pointerOfLinkList = new linkList;
pointerOfLinkList->getData();
pointerOfLinkList->insert(&head,&tail);
cout << "Inserted" << endl;
break;
case 2:
pointerOfLinkList->printAll(head);
break;
case 8:
break;
case 9:
break;
case 0:
is.open("nodes of my link list.dat", ios::binary | ios::in);
pointerOfLinkList = new linkList;
is.read(reinterpret_cast<char*>(pointerOfLinkList),sizeof(*pointerOfLinkList));
while (!is.eof()){
pointerOfLinkList->insert(&head,&tail);
pointerOfLinkList = new linkList;
is.read(reinterpret_cast<char*>(pointerOfLinkList),sizeof(*pointerOfLinkList));
}
is.close();
break;
default:
cout << "Wrong Key pressed" << endl;
}
_getch();
system("cls");
}
return 0;
}