- #1
yungman
- 5,755
- 293
Hi
I have a funky problem I have been working on for over a day, it is supposed to be quite simple, but I have funny result. What the program does is to pick out one of the five records(of structure) stored in the file, modify it and store back into the file in the same position. It supposed to only change the chosen one and leave the others alone. But instead it erase the others. I went through it so many times and I cannot figure this out. This is modification of an exercise from the book. I finally keyed in the exact program from the book, it did the same thing.
There is one program ( 12.20) that create the file with 5 of the records. This program pick one to edit. Because this program destroy the data inside the file, I put exercise 12.20 in line1 to line35. I made it into comment to get it out of the way after creating the Inventory.dat.
I first run the program to read the invItems as shown here before editing. You can see the 5 records.
I used all the tools I know how with debugger. It ALL works UNTIL line 108 to line 110. I verify I wrote in the new data in line 103 to 106 that I got the right data written into record. I edited record #2. But after writing, I closed the program and restart the program so I can read back the 5 records, all the other ones got erased as shown.
Like I said, I finally key in the exercise from the book, it did exactly the same thing. In the exercise, they won't know because it only read back the one they changed. As you see, the #2 record I changed is there. Just the rest got erased.
Now I am at a lost, will line 108 to line 110 erase the other record when editing the chosen one? I thought seekp() and write() only write over the one that is pointing and leave the others alone.
Another thing is, I cannot upload the Inventory.dat here, I even change to .txt and still won't work. Another funny thing is If I create the Inventory.dat with another program and just copy into the folder of this program, It WON'T work. Got to create by this program. Don't ask me why. That's why I finally copy 12.20 onto this program from line 1 to line 35. Use it to create the Inventory.dat for the program to use.
I know this is a long program, I exhausted everything I know how to do and I can't fix it. The problem is really in line 108 to 110.
Please help.
Thanks
I have a funky problem I have been working on for over a day, it is supposed to be quite simple, but I have funny result. What the program does is to pick out one of the five records(of structure) stored in the file, modify it and store back into the file in the same position. It supposed to only change the chosen one and leave the others alone. But instead it erase the others. I went through it so many times and I cannot figure this out. This is modification of an exercise from the book. I finally keyed in the exact program from the book, it did the same thing.
There is one program ( 12.20) that create the file with 5 of the records. This program pick one to edit. Because this program destroy the data inside the file, I put exercise 12.20 in line1 to line35. I made it into comment to get it out of the way after creating the Inventory.dat.
C++:
/*//12.20 create file of blank struct inventory records
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int d_size = 31;
const int numRec = 5;
struct invItem
{
char desc[d_size];
int qty;
double price;
};
int main()
{
fstream inventory;
int index = 0;
invItem record;
cout << " Size of inventory = " << sizeof(invItem) << "\n\n";
inventory.open("Inventory.dat", ios::out | ios::binary);
for (index = 0; index < numRec; index++)
{
cout << " Record " << index << endl;
cout << setw(25) << left << " Description: ";
cin >> record.desc; cout << endl;
cout << setw(25) << left << " Quantity: ";
cin >> record.qty; cout << endl;
cout << setw(25) << left << " Price: " << "$";
cin >> record.price; cout << "\n\n";
inventory.seekp(index * sizeof(invItem), ios::beg);
inventory.write(reinterpret_cast<char*>(&record), sizeof(record));
}
inventory.close();
return 0;
}*/
//12.22 edit record created by 12.20
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int d_size = 31, numRec = 5;
struct invItem
{
char desc[d_size];
int qty;
double price;
};
void displayRec();
void modify(long);
int main()
{
char again, choose; int sel;
//Display all the records in the file
cout << " Do you want to see all records? "; cin >> choose;
if(tolower(choose) == 'y') displayRec();
//Read and edit particular file.
do
{
cout << " What record you want to read and edit? "; cin >> sel; cout << "\n\n";
modify(sel);
cout << " Do you want to edit another record? "; cin >> again;
} while (tolower(again) == 'y');
return 0;
}
void displayRec()
{
fstream inven; invItem rec; long select = 0;
inven.open("Inventory.dat", ios::in | ios::binary);
inven.read(reinterpret_cast<char*>(&rec), sizeof(invItem));
do
{ cout << " Record " << select << endl;
cout << setw(25) << left << " Description: " << rec.desc << endl;
cout << setw(25) << left << " Quantity: " << rec.qty << endl;
cout << setw(25) << left << " Price: " << "$" << rec.price << "\n\n";
inven.read(reinterpret_cast<char*>(&rec), sizeof(invItem));
select++;
} while (!inven.eof());
inven.close();
}
void modify(long index)
{
char choose, again = 'n';fstream inventory;
invItem record; //It's object of struct invItem.
inventory.open("Inventory.dat", ios::in | ios::binary);
inventory.seekg(index * sizeof(invItem), ios::beg);
inventory.read(reinterpret_cast<char*>(&record), sizeof(invItem));
cout << " Record " << index << endl;
cout << setw(25) << left << " Description: " << record.desc << endl;
cout << setw(25) << left << " Quantity: " << record.qty << endl;
cout << setw(25) << left << " Price: " << "$" << record.price << "\n\n";
inventory.close();
cout << " Do you want to change change record " << index << "? ";
cin >> choose; cout << "\n\n";
if (tolower(choose == 'y'))
{
cout << " Record " << index << endl;
cout << setw(25) << left << " Description: "; cin >> record.desc; cout << endl;
cout << setw(25) << left << " Quantity: "; cin >> record.qty; cout << endl;
cout << setw(25) << left << " Price: " << "$"; cin >> record.price; cout << "\n\n";
//Display what you entered
cout << " You entered new data in record " << index << endl;
cout << setw(25) << left << " Description: " << record.desc << endl;
cout << setw(25) << left << " Quantity: " << record.qty << endl;
cout << setw(25) << left << " Price: " << "$" << record.price << "\n\n";
//Write to file
inventory.open("Inventory.dat", ios::out | ios::binary);
inventory.seekp(index * sizeof(record), ios::beg);
inventory.write(reinterpret_cast<char*>(&record), sizeof(record));
inventory.close();
}
}
I used all the tools I know how with debugger. It ALL works UNTIL line 108 to line 110. I verify I wrote in the new data in line 103 to 106 that I got the right data written into record. I edited record #2. But after writing, I closed the program and restart the program so I can read back the 5 records, all the other ones got erased as shown.
Like I said, I finally key in the exercise from the book, it did exactly the same thing. In the exercise, they won't know because it only read back the one they changed. As you see, the #2 record I changed is there. Just the rest got erased.
Now I am at a lost, will line 108 to line 110 erase the other record when editing the chosen one? I thought seekp() and write() only write over the one that is pointing and leave the others alone.
Another thing is, I cannot upload the Inventory.dat here, I even change to .txt and still won't work. Another funny thing is If I create the Inventory.dat with another program and just copy into the folder of this program, It WON'T work. Got to create by this program. Don't ask me why. That's why I finally copy 12.20 onto this program from line 1 to line 35. Use it to create the Inventory.dat for the program to use.
I know this is a long program, I exhausted everything I know how to do and I can't fix it. The problem is really in line 108 to 110.
Please help.
Thanks