By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,417 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,417 IT Pros & Developers. It's quick & easy.

Error reading files in ios::binary mode

P: n/a
Hi developers,
I'm running GCC (GNU C/C++ Compiler shipped with Fedora Core 2). I've
written a program using IOStream classes, in which I'm reading files in
ios::binary mode. I'm using read() method (exact signature not
remembered) of the std::istream class ? When I'm reading the value in
objects attribute (member variable), the read fails, but when I'm
reading in a local variable value is successfully read as shown below:

..... any includes that are necessary

class a {
int magik;
public:
void go(ifstream&);
};

void a::go(ifstream& in) {
int j;

in.read((char*)&magik, sizeof(int)); // Incorrect value read in
magik
}

/*
* If instead of address of a::magik above if I'll pass the local var.
j
* the code works fine.
*/

The above code works fine on Microsoft C/C++ compiler shipped with
Microsoft Visual C++ Toolkit 2003 but fails with MingW GCC 3.4 (same
problem).

Any suggestions,

Thanks,

Ashish Shukla alias Wah Java !!
Wah Java !!

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
WahJava wrote:
Hi developers,
I'm running GCC (GNU C/C++ Compiler shipped with Fedora Core 2). I've
written a program using IOStream classes, in which I'm reading files in
ios::binary mode.


You don't show us the open.

I assume you've determined that sizeof(int) is what you expect.

What does is the state of the stream before the read, after the read?

What characters do you expect, what did you observe.

IT ALSWAYS BEHOOVES YOU WHEN ASKING FOR FREE ADVICE TO:

1. Provide a complete example of the problem.
2. Indicate what you thought should happen.
3. Indicate what you observed happening.
Jul 23 '05 #2

P: n/a
Hi Ron,
Sorry 4 being late. Right now I don't have the code to demonstrate
this. But the objective is I want to read an integer (4 bytes) from a
file opened in binary mode.

But when I'm reading value in local variable code works fine, variable
is read successfully but when reading into class member variable, read
is unsuccessful, the value in the variable, is unpredictable.

The same program works fine with MS Visual C++ Toolkit 2003, but fails
with GCC.

Thanx,

Ashish Shukla alias Wah Java !!
Wah Java !!

Ron Natalie wrote:
WahJava wrote:
Hi developers,
I'm running GCC (GNU C/C++ Compiler shipped with Fedora Core 2). I've written a program using IOStream classes, in which I'm reading files in ios::binary mode.


You don't show us the open.

I assume you've determined that sizeof(int) is what you expect.

What does is the state of the stream before the read, after the read?

What characters do you expect, what did you observe.

IT ALSWAYS BEHOOVES YOU WHEN ASKING FOR FREE ADVICE TO:

1. Provide a complete example of the problem.
2. Indicate what you thought should happen.
3. Indicate what you observed happening.


Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.