Solve Endless Loop C++ File Problem | Banac.dat

  • Context: C/C++ 
  • Thread starter Thread starter chaoseverlasting
  • Start date Start date
  • Tags Tags
    C++ File
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 9K views
chaoseverlasting
Messages
1,051
Reaction score
3
Hi, I am trying to display all the records stored in a file(Banac.dat) but the program keeps on returning the first record over and over again in an endless loop ( i think its endless). If anyone could help me, it would be appreciated.

For the sake of simplicity, I've only included the errenous function and the class definition, if anyone wants to see the whole program, just ask.

Code:
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>char ch;
int ac=0;
int flag=0;
int acn;
float bal;

class account{
    private:
        char name[20], address[20], dob[10], type[2];
        float balance;
        int acno;
    public:
        account(float b=0):balance(b){};
        void create();         
        void close();          
      
        void withdrawal();    
        void deposit();       
      
        void report();        
        void search();         
        void disp();           
    };fstream file;void account::report()
{
    clrscr();
    file.open("Banac.dat", ios::binary|ios::in);
    while(!file)
    {
        file.read((char *)this, sizeof(account));
        disp();
        count<<endl<<endl;
    }
    getch();
    file.close();
}            //End of void report();
 
Last edited by a moderator:
Physics news on Phys.org
...did you mean "while (!file.eof())"?

(I should also mention that dumping binary data into an object's data space is generally regarded as a very poor programming practice, even if it works in this situation. Many things can affect the actual size of an object, including virtual functions, compiler options, etc.)

- Warren
 
Last edited:
read tells you how many bytes it read. You should always check how many bytes you read. :smile:

!file will only be false when an i/o operation has failed.

IIRC, if you're doing something like reading in characters one at a time, it will cause a failure if you try to read past the end of file, and so this test works.

However, read will happily read in zero bytes of data, and return successfully.
 
In C++, you cannot detect the end of file before you go past it. eof() does not return "true" until after you have tried to read past the end of file, and failed.

I've never used the read() member function myself, but I strongly suspect that your loop should be as follows, based on my experience with other input operations in C++, which evaluate as true/false in a Boolean context, indicating whether the operation succeeded or not.

Code:
while (file.read ((char *)this, sizeof(account)))
{
    disp ();
    cout << endl << endl;
}
 
Thank you for checking out my problem. I've tried the solutions offered but though it displays the first record, it keeps on displaying it over and over again. If it is bad programming practice, could you please offer me an alternative solution or point out how I may improve upon the current function.
 
jtbell,

Putting statements in your while loop condition which result in side-effects (i.e. which result in variables changing state) is also considered a very bad programming practice.

chaoseverlasting,

Try storing the return value of read() in a variable, and then printing it out. This way you can see if each call to read() is, in fact, reading any bytes at all.

- Warren
 
chroot said:
Putting statements in your while loop condition which result in side-effects (i.e. which result in variables changing state) is also considered a very bad programming practice.

Nevertheless, it is a very common idiom for input loops in C++. All the C++ input functions and operations (so far as I know) are designed so that they can be used like this. Most of the textbooks that I looked at just now, use this technique. If you want to avoid it, the simplest method is probably to use an "infinite" loop with a "break" statement:

Code:
while (true)
{
    file.read ((char *)this, sizeof(account))
    if (file.eof()) break;
    disp ();
    cout << endl << endl;
}

Otherwise, you have to do something like this:

Code:
file.read ((char *)this, sizeof(account))
while (!file.eof())
{
    disp ();
    cout << endl << endl;
    file.read ((char *)this, sizeof(account))
}

or this:

Code:
do
{
    file.read ((char *)this, sizeof(account))
    if (!file.eof())
   {
        disp ();
        cout << endl << endl;
    }
}
while (!file.eof())