hello all,
am doing an assignment to store records in a text file. i have successfully done the writing part to the file but i got trouble reading from the file in the proper format.
when savin the records to the file i used setiosflag to save the records in kind of tabulated form. i used also used linked list.
here is sample of the code that saves the records to a file
Code:
contact *temp;
temp = conductor;
ofstream extFile("Database.txt", ios::app);
do
{
extFile << setiosflags(ios::left) << setw(20) << temp -> lastname
<< setw(5) << temp -> initial << setw(20) << temp -> firstname
<< setw(25) << temp -> street << setw(20) << temp -> city
<< setw(20) << temp -> state << setw(25) << temp -> country
<< setw(20) << temp -> telephone << setw(25) << temp -> email << endl;
temp = temp -> next;
} while (temp != NULL);
Code used to read from file:
Code:
for (int i = 0; !extFile.eof(); i++)
{
list = new contact;
extFile >> list->lastname;
extFile >> list->initial;
extFile >> list->firstname;
extFile >> list->street;
extFile >> list->city;
extFile >> list->state;
extFile >> list->country;
extFile >> list->telephone;
extFile >> list->email;
list->next=NULL;
if (p == NULL)
{
p = list;
}
else
{
temp_ = p;
while (temp_ ->next != NULL)
{
temp_ = temp_->next;
}
temp_->next = list;
}
}
the variable street consists several strings. so i basically have troubles displaying records after street as the linked list become messed up.
is there a way i can set the predefined number of characters to be read from the file for each record to be stored in the linked list.
also that only the last record is displayed. thank you in anticipation