K
Kamran
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(filename.c_str(),ios::binary);
// filename is defined in the class
cfile->seekg(0, ios::beg);;
cfile->read(header.version,28);
cfile->read((char*)&header.dummy, intSize); // should be 3
cfile->read((char*)&header.Dimension, intSize); // should be 3
cfile->read((char*)&header.dummy, intSize); // should be 0
cfile->read((char*)&header.novalue, floatSize); // should be -9999
cfile->read((char*)&header.xO, floatSize); // should be 0
cfile->read((char*)&header.yO, floatSize); // should be 0
if (header.gridDimension == 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.gridDimension == 3) {
cfile->read((char *)&header.zI, floatSize);
}
for (int i = 0; i < header.gridDimension; i++) {
cfile->read((char *)&header.gNodes, 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
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(filename.c_str(),ios::binary);
// filename is defined in the class
cfile->seekg(0, ios::beg);;
cfile->read(header.version,28);
cfile->read((char*)&header.dummy, intSize); // should be 3
cfile->read((char*)&header.Dimension, intSize); // should be 3
cfile->read((char*)&header.dummy, intSize); // should be 0
cfile->read((char*)&header.novalue, floatSize); // should be -9999
cfile->read((char*)&header.xO, floatSize); // should be 0
cfile->read((char*)&header.yO, floatSize); // should be 0
if (header.gridDimension == 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.gridDimension == 3) {
cfile->read((char *)&header.zI, floatSize);
}
for (int i = 0; i < header.gridDimension; i++) {
cfile->read((char *)&header.gNodes, 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