首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于fstream only reading in 1 structure的问题

关于fstream only reading in 1 structure的问题
EN

Stack Overflow用户
提问于 2014-02-11 12:45:54
回答 1查看 72关注 0票数 0

我有以下问题:我的使用fstream的程序似乎不能在我的.dat文件中写入超过1个数据struct

下面是我的代码,我只能添加一组数据。当我尝试添加另一个时,它起作用了,但没有写出来。知道为什么吗?

例如,我运行此函数,键入详细信息,然后成功地输入了一组预订信息,信息存储在我的.dat文件中。

当我重新运行此功能时,键入详细信息,第二组预订信息没有记录在我的.dat文件中。因此,我的.dat文件最终只有第一个条目,没有存储后续条目

代码语言:javascript
复制
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();

}

这是一些示例输出。只有一个结果,而不是几个。

代码语言:javascript
复制
==========================================================
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
EN

回答 1

Stack Overflow用户

发布于 2014-02-11 15:10:28

没关系,我意识到我错了。

错误是我调用了读,但我只打开了写/追加的文件。

更正后的代码片段将如下所示。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21693332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档