swoopy,
you rock.
it must have been other lines of faulty code when i first tried.
below works now and I used the endl wich trimed down the file size too.
Code:
outstream<<current->fname<<endl;
outstream<<current->lname<<endl;
outstream<<current->ssnumber<<endl;
outstream<<current->age<<endl;
outstream<<current->weight<<endl;
[Edit]
The below code is my working code, but I tried to rewrite the read to resemble the write to eliminate unneeded code, but it didn't work. Please compare the working with my non-working version located underneith it to see if you have any input. Thanks.
basically what all the crap below is saying is that i'm having problems reading (instream>>newlink->fname;) It requires me to loop it and increment each individual location of the array to input the read info. Should it not allow the simple read like the write does?
[/Edit]
Code:
void linklist::write()
{
ofstream outstream("members.dat",ios::binary,ios::app);
if (isempty())
cout << "No nodes to write....";
else
{
link *current;
current = head;
while (current != NULL)
{
outstream<<current->fname<<endl;
outstream<<current->lname<<endl;
outstream<<current->ssnumber<<endl;
outstream<<current->age<<endl;
outstream<<current->weight<<endl;
current = current->next;
}
}
outstream.close();
}
void linklist::read()
{
initializeData();
ifstream instream("members.dat",ios::in);
if (!instream)
cout << "File could not be opened\n....";
else
{
while (!instream.eof())
{
link* newlink = new link;
initializeData();
instream>>buffer1>>buffer2>>buffer3;
instream>>weight>>age;
//;
newlink->age=age;
newlink->weight=weight;
for(x=0;x<SIZE;x++)
{
newlink->fname[x] = buffer1[x];
}
for(x=0;x<SIZE;x++)
{
newlink->lname[x] = buffer2[x];
}
for(x=0;x<SIZE;x++)
{
newlink->ssnumber[x] = buffer3[x];
}
newlink->next = head;
if (head == NULL) //
{
head = newlink;
tail = head;
}
else
{
head = newlink;
}
}
}
i tried to alter the read to clean it up like the write, but it doesn't like it at all. Se the alterations I made below.
Code:
void linklist::read()
{
initializeData();
ifstream instream("members.dat",ios::in);
if (!instream)
cout << "File could not be opened\n....";
else
{
while (!instream.eof())
{
link* newlink = new link;
initializeData();
/*instream>>buffer1>>buffer2>>buffer3;
instream>>weight>>age;
//;
newlink->age=age;
newlink->weight=weight;*/
instream>>newlink->fname;
instream>>newlink->lname;
instream>>newlink->ssnumber;instream>>age;instream>>weight;
/*for(x=0;x<SIZE;x++)
{
newlink->fname[x] = buffer1[x];
}
for(x=0;x<SIZE;x++)
{
newlink->lname[x] = buffer2[x];
}
for(x=0;x<SIZE;x++)
{
newlink->ssnumber[x] = buffer3[x];
}*/
newlink->next = head;
if (head == NULL)
{
head = newlink;
tail = head;
}
else
{
head = newlink;
}
}
}
instream.close();
}
the out put shows garbage in the first print.
see the output below. I want it to be more stream lined, but it looks like the only option is to go back to the working code.
why can I not read the same as write?
-----------------------------------------------------------------------------------
Build and Display a linked list
1) Add an Item to the front of the list
2) Add an Item to the end of the list
3) Display the list
4) Number of nodes in the list
5) Create an ordered list
6) Delete a node (node n)
7) Delete a node (by key)
8) Destroy the list
9) Insert a node anywhere
10) Write to file
11) Read file
12) Quit this stuff
Enter an option: 3
The linked list looks like this so far
Head ->
First Name:
¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T ¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T ¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T ¨T¨T¨T¨T¨T
¨T
Last Name:
¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T ¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T ¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T ¨T¨T¨T¨T¨T
¨T
Social Security #:
¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T ¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T ¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T¨T ¨T¨T¨T¨T¨T
¨T
Age
-12851
Weight
-12851
->
First Name:
john
Last Name:
doe
Social Security #:
123-12-1234
Age
-12851
Weight
-12851
->
First Name:
jane
Last Name:
doe
Social Security #:
123-65-5432
Age
-12851
Weight
-12851
-> NULL
Press Enter to continue....
-----------------------------------------------------------------------------------