ios reading problem

I am having a problem reading in plain binary data from a file using ios.

I use the funciton and it starts off OK, but about 100 bytes into the data, it sets one of the error flags.  I call ios.good() and it returns a non-zero.

However, when I look at the values for the 4 possible error flags: ios::failbit, ios::badbit, ios::eofbit, and ios::goodbit, they are 0,1,2, and 4, but the ios.rdstate() function returns a 3???  Does anyone know what might be happening here??  

The code was originally written for UNIX and seemed to work fine  there, but now I am porting to PC using MSVC++ compiler and it won't read in all the data.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I don't really know, but it may be that the state returned is the (bitwise) ORing of the flags; and you know the error NOT by comparing but by (bitwise) ANDing.

So that could be both failbit and badbit or some other combination.

to see if badbit is on, you do xxx.rdstate() & ios::badbit
cum is right in that.  Although that doesn't explain the original problem.

It might help if you post your code so we might see what you are doing wrong.
ios::badbit | ios::eofbit
Just checking, you did open in binary mode
// This is an example of writing and reading binary data
#include <fstream.h>
void main()
    float f = 3.2;
    ofstream ofile("test.txt", ios::out | ios::binary);
    ofile.write((char*) &f, sizeof(f));

    ifstream ifile("test.txt", ios::in | ios::binary);*) &f, sizeof(f));

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.