[Webinar] Streamline your web hosting managementRegister Today


ios reading problem

Posted on 1999-11-08
Medium Priority
Last Modified: 2010-04-01
I am having a problem reading in plain binary data from a file using ios.

I use the ios.read() 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.

Question by:klingej

Expert Comment

ID: 2192088
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
LVL 22

Expert Comment

ID: 2192886
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.

Expert Comment

ID: 2193321
ios::badbit | ios::eofbit
Just checking, you did open in binary mode

Accepted Solution

tdubroff earned 150 total points
ID: 2291369
// 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);
    ifile.read((char*) &f, sizeof(f));

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question