0

I am trying to read structures from a file using c++, I am using end of file function. But I am having a problem, my last structure is displayed twice. I dont know why is this so.

void display_account(account a) {
    ifstream fin;
    fin.open("bank account.dat",ios::binary);
    if (fin.fail()) {
        cout << "error in opening file.... FILE DONT EXIST ...OR YOU HAVENT CREATED ANY ACCOUNT YET" << endl;
        exit(1);
    }
    else {
        while (!fin.eof()) {
           fin.read((char*)&a, sizeof(a))

                cout << "\nAccount number             :   " << a.AccNumber << endl;
                cout << "Account title              :   " << a.name << endl;
                cout << "State name is              :   " << a.state << endl;
                cout << "Zip code is                :   " << a.zip << endl;
                cout << "Telephone no is            :   " << a.telp << endl;
                cout << "Account balence is         :   " << a.balence << endl;
                cout << "last date of payment is    :   " << a.paymentdt << endl;
            }
        }
    fin.close();
    system("pause");
}
  • 1
    Doesn't look like you fell into the usual trap of `while (!fin.eof())`. Program could be broken if `account` isn't readable with an unformatted read, but I don't think we can successfully answer the question without more information or a guess. Consider producing a [mre]. MRE is an excellent debugging tool and often leads to finding the error by removing everything around the error until you are left with a program that is nothing but error. – user4581301 Mar 25 '21 at 07:39
  • Could the last record actually exist in the file? Did you control the file size and examine the file with a hex editor (like vim)? – Serge Ballesta Mar 25 '21 at 07:54
  • @SergeBallesta Yes, Receord exist, acutally there's another function which write receord.. I created 2 accounts , and Its present in file. – Usama Jalal Mar 26 '21 at 12:39
  • @user4581301 actually if I replace !fin.eof() with (char*)&a, sizeof(a)) , and it does good, But I am still confused why !fin.eof() duplicate last structure. – Usama Jalal Mar 26 '21 at 12:43
  • @user4581301 take a look at the code now, Actually i accidently paste updated code without eof , now I paste my older version of code, I dont know why that wasnt working – Usama Jalal Mar 26 '21 at 12:52
  • @SergeBallesta Actually i accidently paste updated code without eof , now I paste my older version of code, I dont know why that wasnt working – Usama Jalal Mar 26 '21 at 12:53
  • 1
    [Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())) considered wrong?](https://stackoverflow.com/q/5605125/3545273). The eof condition is only set **after the first read that returns no values**, and previous values are left unchanged. So you write once again the last record. The linked post explains it with much more details. – Serge Ballesta Mar 26 '21 at 12:58
  • @SergeBallesta i Think i got it, So if I cut fin.read((char*)&a, sizeof(a)) and paste it just above while loop , then I think my last structure would not be repeated. – Usama Jalal Mar 26 '21 at 13:02

0 Answers0