我遇到以下问题:使用fstream
的程序似乎无法在我的.dat文件中写入多于1个数据struct
。
下面是我的代码,我只能添加1组数据。当我试图添加另一个时,它工作但没有写出来。知道为什么吗?
例如,我运行此功能,输入详细信息,并成功输入1组预订信息,信息存储在我的.dat文件中。
当我重新运行此功能时,输入详细信息,第二组预订信息不会记录在我的.dat文件中。因此,我的.dat文件最终只有第一个条目,而不是存储后续条目
void booking::bkForm(fstream& afile, bForm& b)
{
customer c;
GM g;
GM::holidayPackages h;
char fname [30];
char lname [30];
char address [50];
char date [30];
char req [100];
int pid, position, choice;
bool iStat = false;
bool dStat = false;
int noOfRecords = getNoBFRecords(afile);
int q = g.getNoOfHRecords(afile);
cout << "Please Fill Up Required Booking Form." << endl;
cout << "Please enter your first name: ";
cin.clear();
cin.ignore(100, '\n');
cin.getline(fname, 30);
cout << "Please enter your last name: ";
cin.getline(lname,30);
cout << "Please enter your address: ";
cin.getline(address,50);
cout << "\nThese are the available Packages to choose from." << endl;
g.printHolidayPackages(afile, h);
afile.open("holidayPackages.dat", ios::in | ios::binary);
while(iStat == false)
{
cout << "\nPlease enter the Package ID of the tour that you want to join." << endl;
cout << "Package ID: ";
cin >> pid;
for(int i = 0; i < q; i++)
{
afile.read (reinterpret_cast <char *>(&h), sizeof(h));
if(pid == h.holID)
{
iStat = true;
position = i;
}
}
if(iStat == false)
{
cout << "ID not found, please enter valid Package ID" << endl;
}
}
while(choice!=1 && choice!=2)
{
afile.seekg ((position) * sizeof (GM::holidayPackages), ios::beg);
afile.read (reinterpret_cast <char *>(&h), sizeof (h));
cout << "\nPleasse choose the Date of tour that you want to join." << endl;
cout << "1) " << h.holDate1 << endl;
cout << "2) " << h.holDate2 << endl;
cout << "Your choice: ";
cin >> choice;
if(choice == 1)
{
strcpy(date, h.holDate1);
}
else
{
strcpy(date, h.holDate2);
}
}
cout << "\nPlease State Any Special Requirement That You Have Below." << endl;
cin.clear();
cin.ignore(100, '\n');
cin.getline(req, 100);
afile.close();
afile.open("bookingInfo.dat", ios::out | ios::app | ios::binary);
strcpy(b.bfName, fname);
strcpy(b.blName, lname);
strcpy(b.bAddress, address);
strcpy(b.bDate, date);
strcpy(b.bStatus, "Unchecked");
strcpy(b.bReq, req);
b.packageID = pid;
srand(time(NULL));
int a = rand () % 100000+899990; // random 6 digit number as booking ref.
for(int k = 0; k < noOfRecords; k++)
{
afile.read (reinterpret_cast <char *>(&b), sizeof(b));
while(a == b.bookingRef)
{
a = rand () % 100000+899990;
}
}
b.bookingRef = a;
cout << "Booking Submitted." << endl;
cout << "Please take down the booking reference no : " << b.bookingRef << endl;
cout << "You will need the reference number to check the status of your booking" << endl;
afile.write(reinterpret_cast <const char *>(&b), sizeof(b));
afile.close();
}
这是一些示例输出。只有1个结果而不是少数。
==========================================================
Details Of Booking Number: 0
===========================================================
Booking Reference no: 966373
Customer Name: Cheryl Tan
Customer Address: St24 Tampines
Package ID of Package Booked: 9102
Package Date Choosen: 02032014
Special Requirements: none
Booking Status: Unchecked
答案 0 :(得分:0)
没关系,我意识到我的错误。
我要求读取错误,但我只打开了写入/追加文件。
更正的代码片段将是这样的..
afile.open("bookingInfo.dat", ios::in | ios::binary); //changed here to read
srand(time(NULL));
int a = rand () % 100000+899990; // random 6 digit number as booking ref.
for(int k = 0; k < noOfRecords; k++)
{
afile.read (reinterpret_cast <char *>(&b), sizeof(b));
while(a == b.bookingRef)
{
a = rand () % 100000+899990;
}
}
afile.close(); // close read
afile.open("bookingInfo.dat", ios::out | ios::app | ios::binary); // open append
strcpy(b.bfName, fname);
strcpy(b.blName, lname);
strcpy(b.bAddress, address);
strcpy(b.bDate, date);
strcpy(b.bStatus, "Unchecked");
strcpy(b.bReq, req);
b.packageID = pid;
b.bookingRef = a;
cout << "Booking Submitted." << endl;
cout << "Please take down the booking reference no : " << b.bookingRef << endl;
cout << "You will need the reference number to check the status of your booking" << endl;
afile.write(reinterpret_cast <const char *>(&b), sizeof(b));
afile.close(); // close append