Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading binary data without reinterpret_cast

Just because I've never read binary files before I wrote a program that reads binary STL files. I use ifstreams read member that takes a char* a parameter. To cast my struct to a char* I use a reinterpret_cast. But as far as I remember every book about C++ I read said something like "don't use reinterpret_cast except you have to". What would be a better way read binary data, not necessarily direct, but at last into a struct and without reinterpret_cast?

The main function:

std::ifstream in (cmdline[1].c_str(), std::ios::binary);

in.seekg(80, std::ifstream::beg); //skip header

int numTriangle;
in.read (reinterpret_cast<char*>(&numTriangle), sizeof(int)); //determine number of triangles
//create triangle data type and read data
triangle* t = new triangle();
for (int i = 0; i < numTriangle; ++i)  {
    in.read(reinterpret_cast<char*>(t), triangle::size);
    std::cout << *t;  // there's an opertor<< for triangle
}
delete t;

in.close(); //close file read from

And the triangle struct

//attempt to get the right size of a class without structure padding
#pragma pack(push)
#pragma pack(1)

//standard STL triangle data structure
struct triangle {
public:
    float n[3]; //normals, 4*3=12 bytes

    float x[3]; //first point of the triangle, 4*3=12 bytes
    float y[3]; //second point of the triangle, 4*3=12 bytes
    float z[3]; //third point of the triangle, 4*3=12 bytes

    long int a; //attributes, 2 bytes

    static const int size = 12+12+12+12+2; //sum of member variables
    //static const int size = sizeof(n) + sizeof(x) + sizeof(y) + sizeof(z) + sizeof(a);
};
#pragma pack(pop)

(Extra question: #pragma pack(1) doesn't work with cygwins g++-4. How can I determine the size of the struct?)

like image 569
DaClown Avatar asked Mar 01 '10 14:03

DaClown


People also ask

Should I use Reinterpret_cast?

Purpose for using reinterpret_cast It is used when we want to work with bits. If we use this type of cast then it becomes a non-portable product. So, it is suggested not to use this concept unless required. It is only used to typecast any pointer to its original type.

What is the point of Reinterpret_cast?

The reinterpret_cast allows the pointer to be treated as an integral type. The result is then bit-shifted and XORed with itself to produce a unique index (unique to a high degree of probability). The index is then truncated by a standard C-style cast to the return type of the function.

Is Reinterpret_cast safe?

The reinterpret_cast operator performs potentially unsafe type casts. It is most often used to cast a pointer to a different pointer type. Casting a pointer to a different pointer and back is usually safe and yields the original value.


2 Answers

Well, that code looks fine. You are even caring about the padding issue. I don't see how you can avoid casting here. You can do this sequence:

static_cast<char*>(static_cast<void*>(t))

But really, i don't do that in my code. It's just a more noisy way of doing a direct reinterpret_cast to char*. (See casting via void* instead of using reinterpret_cast ).


The struct size can be determined using sizeof. You just have to initialize the static member out of the class inside the .cpp (however, then the compiler doesn't know the value of ::size anymore and can't inline it).
Alternatively, you can write it as a static inline member function. In its body, the class type is considered complete and sizeof (triangle) is allowed. Or you can just use sizeof like you have in the comment, but use the type and not the members (referring to nonstatic members that way is allowed only in C++0x) :

//standard STL triangle data structure
struct triangle {
public:
    float n[3]; //normals, 4*3=12 bytes

    float x[3]; //first point of the triangle, 4*3=12 bytes
    float y[3]; //second point of the triangle, 4*3=12 bytes
    float z[3]; //third point of the triangle, 4*3=12 bytes

    long int a; //attributes, 2 bytes

    static int size() { return sizeof(triangle); } // this way
    static const int size = sizeof(float[3])*4 + sizeof(long int); // or this way
};

However, the second way is not nice since you can easily forget updating it when you add a member.

like image 141
Johannes Schaub - litb Avatar answered Sep 19 '22 01:09

Johannes Schaub - litb


Extra question: Take a look at __attribute__((packed)).

like image 38
Alexander Gessler Avatar answered Sep 23 '22 01:09

Alexander Gessler