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

  • Thread starter Thread starter chaoseverlasting
  • Start date Start date
  • Tags Tags
    C++ File
Click For Summary
The discussion centers on a C++ programming issue where a function repeatedly displays the first record from a binary file, Banac.dat, due to an incorrect loop condition. Participants suggest that the loop should check the success of the read operation instead of the file stream state, recommending the use of `while (file.read((char *)this, sizeof(account)))` for proper iteration. They highlight that using file stream state checks can lead to infinite loops if no data is read. Additionally, it's noted that dumping binary data directly into an object's data space is poor practice, and alternative looping structures are proposed to improve the code. The conversation emphasizes the importance of proper file handling in C++ to avoid such issues.
chaoseverlasting
Messages
1,050
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();
        cout<<endl<<endl;
    }
    getch();
    file.close();
}            //End of void report();
 
Last edited by a moderator:
Technology 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())
 

Similar threads

Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
6K
Replies
10
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
15K
  • · Replies 1 ·
Replies
1
Views
1K