ifstream in Solaris acting strangely

Discussion in 'C++' started by Kamran, Oct 24, 2006.

  1. Kamran

    Kamran Guest

    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
     
    Kamran, Oct 24, 2006
    #1
    1. Advertisements

  2. It sounds to me like a library bug. At a guess based on the symptoms,
    something is mishandling an ungetc.
     
    James Carlson, Oct 24, 2006
    #2
    1. Advertisements

  3. Kamran

    Martin Steen Guest

    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
     
    Martin Steen, Oct 24, 2006
    #3
  4. Kamran

    Larry Smith Guest



    This is a known bug in Forte 6. It is corrected in later versions.
     
    Larry Smith, Oct 24, 2006
    #4
  5. Kamran

    Kamran Guest


    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
     
    Kamran, Oct 27, 2006
    #5
  6. Kamran

    Kamran Guest



    and so it is. Thanks for the answer.

    regards
    kamran
     
    Kamran, Oct 27, 2006
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.