Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ File problem

  1. Aug 16, 2006 #1
    Hi, Im 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 (C):
    #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: Mar 17, 2015
  2. jcsd
  3. Aug 16, 2006 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    ...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: Aug 16, 2006
  4. Aug 16, 2006 #3

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
     
  5. Aug 16, 2006 #4

    jtbell

    User Avatar

    Staff: Mentor

    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 (Text):

    while (file.read ((char *)this, sizeof(account)))
    {
        disp ();
        cout << endl << endl;
    }
     
     
  6. Aug 16, 2006 #5
    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.
     
  7. Aug 16, 2006 #6

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  8. Aug 16, 2006 #7

    jtbell

    User Avatar

    Staff: Mentor

    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 (Text):

    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 (Text):

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

    Code (Text):

    do
    {
        file.read ((char *)this, sizeof(account))
        if (!file.eof())
       {
            disp ();
            cout << endl << endl;
        }
    }
    while (!file.eof())
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++ File problem
  1. C++ file i/o (Replies: 7)

  2. C++ csv file reading (Replies: 2)

Loading...