Aug 4, 2013 at 12:04pm UTC
can anyone help me implement file stream in a linked list?
here are my 3 files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
//#include <iostream>
#ifndef LIST
#define LIST
class List
{
private :
class choco
{
public :
string bName;
double bPrice;
string bMDate;
string bExDate;
choco *next;
public :
choco()
{
bName = "" ;
bPrice = 0.0;
bMDate = "" ;
bExDate = "" ;
next = NULL;
}
choco(string n, double p, string md, string xd)
{
bName = n;
bPrice = p;
bMDate = md;
bExDate = xd;
next = NULL;
}
choco(string n, double p, string md, string xd, choco *nx)
{
bName = n;
bPrice = p;
bMDate = md;
bExDate = xd;
next = nx;
}
string getName()
{return bName;}
double getPrice()
{return bPrice;}
string getMD()
{return bMDate;}
string getXD()
{return bExDate;}
choco *getNext()
{return next;}
void setNext(choco *n)
{next = n;}
};
public :
List();
bool empty() const ;
int countNode();
void display();
void insert(string name, double price, string MD, string XD, int pos);
private :
choco *ptr;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#include<iostream>
using namespace std;
#include "myDriver.h"
List::List()
{ptr = NULL;}
bool List::empty() const
{
if (ptr == NULL)
return true ;
return false ;
}
int List::countNode()
{
int counter = 0;
choco *temp = ptr;
while (temp != NULL)
{
counter++;
temp = temp->getNext();
}
return counter;
}
void List::display()
{
choco *temp = ptr;
if (ptr == NULL)
cout << "NO CHOCOLATE TO BE DISPLAYED" << endl;
int i = 1;
cout << "+============================================+" << endl;
cout << "| CHOCOLATE RECORD |" << endl;
cout << "+============================================+" << endl;
while (ptr != NULL)
{
cout << "+=======================================================+" << endl;
cout << "| CHOCOLATE NUMBER # " << i++ << endl;
cout << "| CHOCOLATE NAME : " << temp->getName() <<endl;
cout << "| PRICE : " << temp->getPrice() << endl;
cout << "| MANUFACTURING DATE : " << temp->getMD() << endl;
cout << "| EXPIRATION DATE : " << temp->getXD() << endl;
cout << "+=======================================================+" << endl;
cout << endl << endl;
ptr = ptr->next;
}
}
void List::insert(string name, double price, string MD, string XD, int pos)
{
int c = countNode();
if (pos < 1 || pos > c + 1)
cout << "CANNOT INSERT THAT RECORD. TRY AGAIN" << endl;
else
{
choco *newptr = new choco(name,price,MD,XD);
if (ptr == NULL && pos == 1)
ptr = newptr;
else if (pos == 1 || ptr != NULL)
{
newptr->setNext(ptr);
ptr = newptr;
}
else if (pos == c+1)
{
choco *predptr = ptr;
while (ptr->getNext() != NULL)
{
predptr = predptr->getNext();
}
predptr->setNext(newptr);
}
else
{
choco *ptr1 = ptr;
choco *ptr2 = ptr;
for (int i = 2; i != pos; i++)
ptr1 = ptr->getNext();
for (int j = 1; j != pos; j++)
ptr2 = ptr->getNext();
choco *ptr3 = new choco(name,price,MD,XD,ptr2);
ptr1->setNext(ptr3);
}
cout << "NEW RECORD INSERTED AT POSITION " << pos << endl;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
#include<fstream>
using namespace std;
#include "myHeader.h"
int main()
{
List l;
string cname;
double cprice;
string cMDate;
string cXDate;
int pos;
char command;
do
{
cout << "+=============================================+" << endl;
cout << "| [a]dd [d]isplay e[x]it |" << endl;
cout << "+=============================================+" << endl;
cin >> command;
switch (command)
{
case 'a' :
ifstream in("input.txt" );
in >> cname >> cprice >> cMDate >> cXDate;
cout << "INSERTING DATA" << endl << endl;
cin.get();
cout << "\t\t CHOCOLATE NAME : " ; getline(in, cname);
cout << "\t\t PRICE : " ; in >> cprice;
cin.get();
cout << "\t\t MANUFACTURING DATE : " ; getline(in, cMDate);
cout << "\t\t EXPIRATION DATE : " ; getline(in, cXDate);
cout << "\t\t ENTER POSITION : " ; in >> pos;
cout << endl;
l.insert(cname, cprice, cMDate, cXDate,pos);
break ;
case 'd' :
l.display();
break ;
}
} while (command != 'x' );
return 0;
}
thanks in advance :)
Last edited on Aug 4, 2013 at 12:08pm UTC
Aug 4, 2013 at 2:04pm UTC
You haven't stated what you want to do with a file stream.
I'm guessing you want to read and write choco instances to/from a file stream.
To write choco instances to an ostream, you're going to want to open the file,
then iterate through your list. For each choco entry, you're going to need a function that serializes (writes) a choco instance to the ostream. Then close the file when you've written all the instances.
Likewise to read from a file, you will need to open the file. Again you will need a function to read a choco instance from the file into a choco instance. You then need to link that choco instnace into your list. And of course when your reach eof, you will need to close the file.