简体   繁体   中英

Reading Binary File into a struct too many times c++

I'm writing a program that reads and writes from a binary file, my problem is that although the file (for example) have 3 structs in it, it reads the last struct twice. this is my code:

the struct:

struct PARTICEPENTS
{
    char id1[10];
    char name1[11];
    char id2[10];
    char name2[11];
    int points;
};

writing:

void EnterParticipants()
{
    ofstream  PartFile;
    int i=1;
    PARTICEPENTS part;
    string temp="";

    PartFile.open("part.bin",ios::out|ios::binary);

    if(!PartFile){return;}

    cout<<endl<<endl <<"Enter 3 Participants:"<<endl;
    cout<<endl;

    while(i<=3)
    {               
        cout<<"the "<<i<<" couple"<<endl;

        cout << "\tInsert the first id: " ;
        getline(cin, temp);
        strcpy(part.id1,stToChar(temp));
        cout << "\tInsert the first name: ";
        getline(cin, temp);
        strcpy(part.name1,stToChar(temp));
        cout << "\tInsert the second id:";
        getline(cin, temp);
        strcpy(part.id2,stToChar(temp));
        cout << "\tInsert the second name:" ;
        getline(cin, temp);
        strcpy(part.name2,stToChar(temp));      
        part.points=0;

        PartFile.write((char*)(&part), sizeof(part));

        i++;
    }

    PartFile.close();
}

reading:

void DisplayFile()
{

    ifstream PartFile;
    PartFile.open("part.bin",ios::in|ios::binary);
    PARTICEPENTS filePart;

    if(!PartFile){return;}


    while(!PartFile.eof())
    {
        PartFile.read((char*)(&filePart), sizeof(filePart));
        cout<<left<<setw(12)<<filePart.id1<<left<<setw(12)<< filePart.name1 
                <<left<<setw(12)<<filePart.id2<<left<<setw(12)<<filePart.name2<<
                left<<setw(7)<< filePart.points <<endl;
    }
}

When I enter:

111111111 Kim 111111111 Lori 
222222222 Roy 222222222 Tom 
333333333 Guy 333333333 Don 

the output to the screen is:

111111111 Kim 111111111 Lori 0
222222222 Roy 222222222 Tom 0
333333333 Guy 333333333 Don 0
333333333 Guy 333333333 Don 0

I have no idea why the reading doesn't stop after the last struct. Thanks for your help. (sorry for my bad English...)

while(!PartFile.eof())
{
    PartFile.read((char*)(&filePart), sizeof(filePart));
    cout<<left<<setw(12)<<filePart.id1<<left<<setw(12)<< filePart.name1 
            <<left<<setw(12)<<filePart.id2<<left<<setw(12)<<filePart.name2<<
            left<<setw(7)<< filePart.points <<endl;
}

The problem here is that you are reading three entries, but you haven't reached the end of file yet (it is the next one when you read the third) . The end of file is reached when you enter the loop for the fourth time.

Then since

PartFile.read((char*)(&filePart), sizeof(filePart));

tries to read the eof in to filePart and fails so nothing is read in to it. Which means it will still contain the data from the third loop. That will be displayed again.

You can fix it by making sure PartFile.read has succeeded before displaying data.

while(PartFile.read((char*)(&filePart), sizeof(filePart)))
{
    // PartFile.read((char*)(&filePart), sizeof(filePart));
    cout<<left<<setw(12)<<filePart.id1<<left<<setw(12)<< filePart.name1 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM