473,836 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2707
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
1310
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. #include <stdio.h> #include <stdlib.h> #include <iostream.h> #include <fstream.h>
103
48792
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 says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
2
2089
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 read_file(const char *fname, std::string &data) { std::ifstream in(fname); if (!in.is_open()) return false;
8
9539
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 contains the following three floating-point numbers: 1.0 2.0 3.0
1
3045
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 structure, but this is very inefficent compared to VB3 where I could use Get method to read the whole structure data? Is there any similar method in .net? -- Message posted via http://www.dotnetmonster.com
9
9561
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; streampos availtable; static fstream availfile; static void openavailfile(bool writemode); int main() { streampos j;
9
9893
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 should loop 15,870,080 times. The code is: newprogram.cpp =============
7
2348
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 hex editor and this is what i am supposed to get: 2EFACCF69FFC84... Can anyone help me in this? Thanks in advance! char *memblock; ifstream::pos_type size; ifstream file;
9
3686
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) { std::cout << "open(" << filename.c_str() << ", std::ios::binary | std::ios::trunc);" << std::endl;
0
9811
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9657
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10822
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.