reading a file
Feb 22, 2009 at 5:13pm UTC
I having trouble with reading the entries. When I select 2: it only reads the entries on the first interation and faults when opening the file with two or more iterations.I'd appreciate any tips.
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
do
{
// display menu
cout << "\nMr. Bigs' Computer shop\n" ;
cout << "-----------------------\n" ;
cout << "1. Add an inventory item\n" ;
cout << "2. View entries\n" ;
cout << "3. Update an existing entry\n" ;
cout << "4. Quit program\n\n" ;
cout << "Enter your choice: " ;
cin >> choice;
if (choice==1){
//call write function
writeEntry(outfile);
}
if (choice==2){
//call read function
readEntry(infile);
}
if (choice==3){
//call edit function: allows for updating existing entry
editEntry(outfile);
}
if (choice==4){
exit(0);
}
}while (choice!=4);
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
void readEntry(fstream &infile)
{
infile.open("inventory.out" , ios::in|ios::binary);
if (!infile)
{
cout << "There was problems with opening the file." << endl;
return ;
}
inventInfo temp;
infile.read(reinterpret_cast <char *>(&temp), sizeof (temp));
while (!infile.eof())
{
cout << "\nItem: " << temp.description << endl;
cout << "Quantity: " << temp.quantity << endl;
cout << "Date: " << temp.date << endl;
infile.read(reinterpret_cast <char *>(&temp), sizeof (temp));
}
infile.close();
}
Topic archived. No new replies allowed.