I've been trying to read this binary file I wrote with a python script. The format of the file is this, first there is an integer that tells how many floating point numbers follow that integer(there will be 5 times this number), then the floats follow, another integer, etc.

I've tried to read this file in a Java program and now I'm trying to do it in this C++ program, and in both cases the first integer and first floats are read correctly, but as the block of floats are being read what is read stops being what I wrote(or at least they aren't the floats written in the text file that gets generated along side the binary one).

What I'm really at a loss to understand though is that as I'm reading the first floats I reach the end of the file, which seems impossible to me. The file is 28kB and I read at most 1 integer and 5*38=190 floats, which comes out to only 764 bytes. Also uncommenting the tellg() lines does something else I don't understand, the first line prints 0, the second one(after I've read 4 bytes) prints 346, and the programs reads more floats(all of it gibberish), which seems to suggest that tellg() is changing something instead of join reading the position in the file. It might be a problem with my file but I thought it would be worth it to have someone look at my code.

#include <stdlib.h>
#include <fstream>
#include <iostream>

using namespace std;

void reverseBytes(char *bytes, int count){
    int halfCount = count / 2;
    char temp;

    for(int a = 0;a < halfCount;a++){
        temp = bytes[a];
        bytes[a] = bytes[count-1-a];
        bytes[count-1-a] = temp;
    }

    return;
}

int main(int argc, char** argv) {
    ifstream file("monkey.c3d");
    cout << file.tellg() << endl;

    int count;
    float x, y, z, u, v;

    file.read((char*)&count, sizeof(int));
    reverseBytes((char*)&count, 4);
    cout << file.tellg() << endl;
    cout << count << endl;

    for(int a = 0;a < count;a++){
        if(file.eof()){
            cout << "End of File" << endl;
            break;
        }
        file.read((char*)&x, sizeof(float));
        reverseBytes((char*)&x, sizeof(float));
        file.read((char*)&y, sizeof(float));
        reverseBytes((char*)&y, sizeof(float));
        file.read((char*)&z, sizeof(float));
        reverseBytes((char*)&z, sizeof(float));
        file.read((char*)&u, sizeof(float));
        reverseBytes((char*)&u, sizeof(float));
        file.read((char*)&v, sizeof(float));
        reverseBytes((char*)&v, sizeof(float));

        cout << x << ", " << y << ", " << z << ", " << u << ", " << v << endl;
    }

    file.close();

    return (EXIT_SUCCESS);
}

line 21: That is opening the file in text mode, not binary mode. You need to add ios::binary flag ifstream file("monkey.c3d", ios::binary); That is most likely why you get the unexpected problems you are experiencing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.