我遇到問題附加到隨機訪問文件。我似乎無法覆蓋文件中的字段。相反,我只追加到文件中,並保留原來的名稱。附加隨機訪問文件
fstream Airline("AirLine.text", ios::out | ios::in | ios::app | ios::binary);
if(!Airline)
{
cout << "File Could not be opened." << endl;
system("PAUSE");
exit (1);
}
cout << "Enter Seat Number to Update" << "(1 to 100, 0 to end)?";
NewPassenger passenger;
cin >> passenger.seat_number;
while (0 < passenger.seat_number && passenger.seat_number <= 100)
{
Airline.seekp((passenger.seat_number - 1) * sizeof(NewPassenger));
Airline.read(reinterpret_cast<char *>(&passenger), sizeof(NewPassenger));
cout << " Found Record. Now Enter New Name:";
cin >> passenger.name;
Airline.seekg((passenger.seat_number - 1) * sizeof(NewPassenger),ios::beg);
Airline.write(reinterpret_cast<const char *>(&passenger), sizeof(NewPassenger));
cout << "\nEnter Seat Number: ?";
cin >> passenger.seat_number;
}
Airline.close();
cout << endl;
NewPassenger
定義如下:
struct NewPassenger
{
int seat_number;
char name[20];
char onward[3];
};
謝謝我改變應用程序去吃,它工作..這是我的頭在約4小時... – user1058717