2014-02-11 35 views
0

我遇到以下問題:使用fstream的程序似乎無法將超過1個數據struct寫入我的.dat文件。有關fstream只能讀取1個結構的問題

下面是我的代碼,我只能夠增加1組數據。當我試圖添加其他,它的工作,但沒有寫出來。任何想法爲什麼?

例如,我運行此功能,輸入詳細信息,併成功輸入了1套預訂信息,信息存儲在我的.dat文件中。

當我重新運行該功能,輸入詳細信息,第2集的預訂信息不被記錄在我的.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(); 

} 

t這是一些示例輸出。只有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

它的好,我意識到我的錯誤。

錯誤是我要求讀,但我只打開寫入/附加文件。

校正代碼片段將是這樣..

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