473,779 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifstream in Solaris acting strangely


Hi,

I am having problem using std::ifstream in my code under solaris.
Everything works fine on linux but the same thing i.e.
trying to read a binary file and fill in a struct result in
complete junk. I use WorskShop 6.0 and my solaris version
is 9.

An "od -x" on my input file gives:

0000: 68 65 61 72 74 5f 70 72 6f 70 65 72 74 79 5f 76
0010: 65 72 73 69 6f 6e 5f 30 32 2e 30 30 00 00 00 03
0020: 00 00 00 03 00 00 00 00 c6 1c 3c 00 00 00 00 00
0030: 00 00 00 00 bc 23 d7 0a 3d cc cc c4 3d cc cc c4
0040: 3d cd 17 a9 00 00 00 65 00 00 00 65 00 00 00 47

I have the following struct:

struct cubeHeader
{
char version[28];
int Dimension;
float novalue;
float xO;
float yO;
float zO;
float xIn;
float yIn;
float zIn;
int gNodes[3];
int dummy;
};

cubeHeader header;
I open the binary file and read using:

#include <string>
#include <iostream>
#include <fstream>

cfile = new std::ifstream(f ilename.c_str() ,ios::binary);
// filename is defined in the class

cfile->seekg(0, ios::beg);;
cfile->read(header.ve rsion,28);
cfile->read((char*)&h eader.dummy, intSize); // should be 3
cfile->read((char*)&h eader.Dimension , intSize); // should be 3
cfile->read((char*)&h eader.dummy, intSize); // should be 0
cfile->read((char*)&h eader.novalue, floatSize); // should be -9999
cfile->read((char*)&h eader.xO, floatSize); // should be 0
cfile->read((char*)&h eader.yO, floatSize); // should be 0
if (header.gridDim ension == 3) {
cfile->read((char *)&header.zO, floatSize); // should be -0.1
}
cfile->read((char *)&header.xI, floatSize);
cfile->read((char *)&header.yI, floatSize);
if (header.gridDim ension == 3) {
cfile->read((char *)&header.zI, floatSize);
}
for (int i = 0; i < header.gridDime nsion; i++) {
cfile->read((char *)&header.gNode s[i], intSize);
}
What I get is that the first byte in the file is read twice, i.e. my
header.version' s byte 0 and 1 both have value 68 (see the input file
at the top) and the subsequent bytes are then (presumably) read wrong.
(it is infact complete bullocks)
Then I thought well I don't start from position 0 but position 1:
seekg(1, ios::beg). The result was that it didn't read the first byte
(as intended) but read the second byte twice (value 65). Still stranger,
those integer variables that followed the "version" part of the header
was read correctly but not the float variables !!!
I hate to admit that I am doing something stupid here though it often
turns out to be the case but could it be some compiler incompatibilty or
version or solaris and std library ? Any excuse at all ?

Thanks in advance

Kamran

Oct 24 '06 #1
5 2684
Kamran <ka****@uio.now rites:
What I get is that the first byte in the file is read twice, i.e. my
header.version' s byte 0 and 1 both have value 68 (see the input file
at the top) and the subsequent bytes are then (presumably) read wrong.
(it is infact complete bullocks)
Then I thought well I don't start from position 0 but position 1:
seekg(1, ios::beg). The result was that it didn't read the first byte
(as intended) but read the second byte twice (value 65). Still stranger,
those integer variables that followed the "version" part of the header
was read correctly but not the float variables !!!
I hate to admit that I am doing something stupid here though it often
turns out to be the case but could it be some compiler incompatibilty
or
version or solaris and std library ? Any excuse at all ?
It sounds to me like a library bug. At a guess based on the symptoms,
something is mishandling an ungetc.

--
James Carlson, KISS Network <ja************ *@sun.com>
Sun Microsystems / 1 Network Drive 71.232W Vox +1 781 442 2084
MS UBUR02-212 / Burlington MA 01803-2757 42.496N Fax +1 781 442 1677
Oct 24 '06 #2
Kamran wrote:
>
Hi,

I am having problem using std::ifstream in my code under solaris.
Everything works fine on linux but the same thing i.e.
trying to read a binary file and fill in a struct result in
complete junk. I use WorskShop 6.0 and my solaris version
is 9.

An "od -x" on my input file gives:

0000: 68 65 61 72 74 5f 70 72 6f 70 65 72 74 79 5f 76
0010: 65 72 73 69 6f 6e 5f 30 32 2e 30 30 00 00 00 03
0020: 00 00 00 03 00 00 00 00 c6 1c 3c 00 00 00 00 00
0030: 00 00 00 00 bc 23 d7 0a 3d cc cc c4 3d cc cc c4
0040: 3d cd 17 a9 00 00 00 65 00 00 00 65 00 00 00 47

I have the following struct:

struct cubeHeader
{
char version[28];
int Dimension;
float novalue;
float xO;
float yO;
float zO;
float xIn;
float yIn;
float zIn;
int gNodes[3];
int dummy;
};

cubeHeader header;
I open the binary file and read using:

#include <string>
#include <iostream>
#include <fstream>

cfile = new std::ifstream(f ilename.c_str() ,ios::binary);
// filename is defined in the class

cfile->seekg(0, ios::beg);;
cfile->read(header.ve rsion,28);
cfile->read((char*)&h eader.dummy, intSize); // should be 3
cfile->read((char*)&h eader.Dimension , intSize); // should be 3
cfile->read((char*)&h eader.dummy, intSize); // should be 0
cfile->read((char*)&h eader.novalue, floatSize); // should be -9999
cfile->read((char*)&h eader.xO, floatSize); // should be 0
cfile->read((char*)&h eader.yO, floatSize); // should be 0
if (header.gridDim ension == 3) {
cfile->read((char *)&header.zO, floatSize); // should be -0.1
}
cfile->read((char *)&header.xI, floatSize);
cfile->read((char *)&header.yI, floatSize);
if (header.gridDim ension == 3) {
cfile->read((char *)&header.zI, floatSize);
}
for (int i = 0; i < header.gridDime nsion; i++) {
cfile->read((char *)&header.gNode s[i], intSize);
}
What I get is that the first byte in the file is read twice, i.e. my
header.version' s byte 0 and 1 both have value 68 (see the input file
at the top) and the subsequent bytes are then (presumably) read wrong.
(it is infact complete bullocks)
Then I thought well I don't start from position 0 but position 1:
seekg(1, ios::beg). The result was that it didn't read the first byte
(as intended) but read the second byte twice (value 65). Still stranger,
those integer variables that followed the "version" part of the header
was read correctly but not the float variables !!!
I hate to admit that I am doing something stupid here though it often
turns out to be the case but could it be some compiler incompatibilty or
version or solaris and std library ? Any excuse at all ?

Thanks in advance

Kamran
This is a known bug in Forte 6. It is corrected in later versions.
Oct 24 '06 #3
Kamran wrote:
>
Hi,

I am having problem using std::ifstream in my code under solaris.
Everything works fine on linux but the same thing i.e.
trying to read a binary file and fill in a struct result in
complete junk. I use WorskShop 6.0 and my solaris version
is 9.
Did you take care of the byte order?

Sun SPARC has a different byte order than Intel x86.
SPARC = Big endian or MSB, Intel x86 = little endian or LSB.

http://en.wikipedia.org/wiki/Byte_order

Best regards,
-Martin
Oct 25 '06 #4
Martin Steen wrote:
Kamran wrote:
>>
Hi,

I am having problem using std::ifstream in my code under solaris.
Everything works fine on linux but the same thing i.e.
trying to read a binary file and fill in a struct result in
complete junk. I use WorskShop 6.0 and my solaris version
is 9.


Did you take care of the byte order?

Sun SPARC has a different byte order than Intel x86.
SPARC = Big endian or MSB, Intel x86 = little endian or LSB.

http://en.wikipedia.org/wiki/Byte_order

Best regards,
-Martin

Thanks for the answer Martin. Yes I have made sure about that. But the
problem seems to be WS Forte 6.0 on Solaris. It is a bug there that I
have been told to have been fixed in the later versions. The order for
this has been placed and in the meantime I have to use the good old
FILE* in C.

regards

Kamran

Oct 27 '06 #5
Larry Smith wrote:
Kamran wrote:
>>Hi,

I am having problem using std::ifstream in my code under solaris.
Everything works fine on linux but the same thing i.e.
trying to read a binary file and fill in a struct result in
complete junk. I use WorskShop 6.0 and my solaris version
is 9.

An "od -x" on my input file gives:

0000: 68 65 61 72 74 5f 70 72 6f 70 65 72 74 79 5f 76
0010: 65 72 73 69 6f 6e 5f 30 32 2e 30 30 00 00 00 03
0020: 00 00 00 03 00 00 00 00 c6 1c 3c 00 00 00 00 00
0030: 00 00 00 00 bc 23 d7 0a 3d cc cc c4 3d cc cc c4
0040: 3d cd 17 a9 00 00 00 65 00 00 00 65 00 00 00 47

I have the following struct:

struct cubeHeader
{
char version[28];
int Dimension;
float novalue;
float xO;
float yO;
float zO;
float xIn;
float yIn;
float zIn;
int gNodes[3];
int dummy;
};

cubeHeader header;
I open the binary file and read using:

#include <string>
#include <iostream>
#include <fstream>

cfile = new std::ifstream(f ilename.c_str() ,ios::binary);
// filename is defined in the class

cfile->seekg(0, ios::beg);;
cfile->read(header.ve rsion,28);
cfile->read((char*)&h eader.dummy, intSize); // should be 3
cfile->read((char*)&h eader.Dimension , intSize); // should be 3
cfile->read((char*)&h eader.dummy, intSize); // should be 0
cfile->read((char*)&h eader.novalue, floatSize); // should be -9999
cfile->read((char*)&h eader.xO, floatSize); // should be 0
cfile->read((char*)&h eader.yO, floatSize); // should be 0
if (header.gridDim ension == 3) {
cfile->read((char *)&header.zO, floatSize); // should be -0.1
}
cfile->read((char *)&header.xI, floatSize);
cfile->read((char *)&header.yI, floatSize);
if (header.gridDim ension == 3) {
cfile->read((char *)&header.zI, floatSize);
}
for (int i = 0; i < header.gridDime nsion; i++) {
cfile->read((char *)&header.gNode s[i], intSize);
}
What I get is that the first byte in the file is read twice, i.e. my
header.versio n's byte 0 and 1 both have value 68 (see the input file
at the top) and the subsequent bytes are then (presumably) read wrong.
(it is infact complete bullocks)
Then I thought well I don't start from position 0 but position 1:
seekg(1, ios::beg). The result was that it didn't read the first byte
(as intended) but read the second byte twice (value 65). Still stranger,
those integer variables that followed the "version" part of the header
was read correctly but not the float variables !!!
I hate to admit that I am doing something stupid here though it often
turns out to be the case but could it be some compiler incompatibilty or
version or solaris and std library ? Any excuse at all ?

Thanks in advance

Kamran


This is a known bug in Forte 6. It is corrected in later versions.
and so it is. Thanks for the answer.

regards
kamran

Oct 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13780
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is the easiest way to take a huge project across onto it. Basically, I've hit a problem. I have no idea how the ifstream class handles directories. In the code I have (which I didn't write), there are several places whereby an ifstream stream is...
6
3396
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber" 10,20,30
4
3739
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream in("in.txt"); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";} in.close(); if(!in) {cout << "error\n";}
10
18025
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
8
3388
by: ambar.shome | last post by:
Hi, We are working on Solaris SunOS 5.8. We are using ifstream of iostream namespace. The snippet is given below: #include <iostream.h> #include <fstream.h> boolean readFile() { char readStr;
1
2887
by: tinks | last post by:
I am getting a linking error when I do something like this: ifstream dataFile; dataFile.open(dataFileName_, ios::in); while(dataFile) { dataFile.getline(buffer, MAX_DATA_FILE_LINE_LEN); // This line creates linking issue on solaris }
2
28710
by: manwanirg | last post by:
the function getline is a public member of istream and cin.getline can be used. Since ifstream is publicily derived from istream, getline shall be available in ifstream as well. However,on solaris ver 2.7, getline is not recognised. e.g ifstream f("a.cpp"); string s; f.getline(s,100); why doesn't complier recognise fstream::getline(string,int) even though istream::getline(string,int) is recognized
5
3579
by: brad | last post by:
How would I determine the number of bytes that is.read actually read? // allocate memory char * buffer; while (!is.eof()) { buffer = new char ;
0
1808
by: mg | last post by:
When make gets to the _ctypes section, I am getting the following in my output: building '_ctypes' extension creating build/temp.solaris-2.10-i86pc-2.5/home/ecuser/Python-2.5.1/ Modules/_ctypes creating build/temp.solaris-2.10-i86pc-2.5/home/ecuser/Python-2.5.1/ Modules/_ctypes/libffi creating build/temp.solaris-2.10-i86pc-2.5/home/ecuser/Python-2.5.1/ Modules/_ctypes/libffi/src
0
9632
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
9471
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
10302
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
10071
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
8958
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...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
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...
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.