473,320 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Error reading files in ios::binary mode

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
2 2672
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dimitri Papoutsis | last post by:
Hi there, ... i want to read in a binary data file and to cast each pair of 2 bytes to a signed short value. i try to different ways, but none results in what i suppose it should result. ...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
2
by: Jason Heyes | last post by:
I tried to use this function to read a binary file. It only reads a few thousand bytes and not the entire file. How do I change it to read a whole binary file? Thanks in advance. bool...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
1
by: Manjunath sp via DotNetMonster.com | last post by:
Hi, How to effectively write and read structures from binary files in .Net? Currently I am using functions like ReadInt32 and the likes to read data from binary file into each elements of a...
9
by: vermarajeev | last post by:
Hi @all, I'm trying to read a binary file containing some data for my cross platform project. Here is the code snippet which will help me explain my problem const int AVAILSIZE = 100000;...
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
7
by: UnknownBlue | last post by:
Hi. I am trying to read .dat files which is in binary using visual C++ 6.0. I want to display the first 100 char (in hex). But instead some weird char are being display. I opened the file through...
9
by: Ron Eggler | last post by:
Hi, I would like to write binary data in a file i open (ofstream) with ios::binary but it keeps failing and it gives me a segmentation fault. What I'm exactly doing: if (isBinary == true) {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.