9

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?)

DaClown
  • 4,171
  • 6
  • 31
  • 31
  • 1
    If you are reading/writing binary data it is a good idea to use reinterpret_cast. This is because the code is inherently non portable and thus reinterpret_cast is a good way of self documenting the code to indicate the code is non portable. Note: There is nothing wrong with your code. It is non portable because of the way C/C++ define the basic types and their layout (which can be different across hardware/OS/Compiler/Compiler flags). – Martin York Mar 01 '10 at 16:22

3 Answers3

8

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.

Community
  • 1
  • 1
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Thanks. I forgot to delete the second line, it was late that day I wrote this. However, my cygwins gcc/g++/... seems broken, neither sizeof, nor sizeof+pragma pack, nor sizeof __attribute__((pack)) works. – DaClown Mar 01 '10 at 16:48
  • 1
    Note that doing state serialization like this is inherently unportable - trying to read a file on a different architecure (or even the same one with a different compiler) may yield a garbage data restoration. Also I haven't seen a system where long was two bytes in a very long time - most likely it will be four or eight bytes spending on processor and compiler settings. Edit: What do you mean that those things don't work? Did you try removing the pragma pack and the static size function suggested? – Mark B Mar 01 '10 at 16:52
  • In fact, `long` *must* be greater than 2 bytes on any system that has 8bit bytes. – Johannes Schaub - litb Mar 01 '10 at 16:57
  • You could always move the calculation of `size` into a separate traits class. Then you get the best of both worlds and remove the concern of serialization from the class itself. – MSN Mar 01 '10 at 17:09
  • Yes, this `long` was the missing link. Of course it should have been a `short int`. Thanks – DaClown Mar 01 '10 at 17:34
2

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

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
-2

Using streams for file i/o (esp. binary) is just nasty in my opinion. I'd rather just use the old C functions like fopen and fread if I were you.

Also, memory mapping of a file is a technique which is given too little love, IMO. I don't know of any standard/portable libraries that support it, but if you're on Windows I suggest checking this MSDN article

Rune Aamodt
  • 2,551
  • 2
  • 23
  • 27
  • 2
    Any substantiation at all? Why are the standard C++ functions "nasty"? What do they lack? What do the ancient C versions do better? I'm very sceptical. I've had no trouble using C++ fstreams to read files over carefully mapped structs and do all kinds of bit-accurate stuff. Their benefits of OO, RAII, exception handling, and presumably more, are very significant. This is more like an opinionated rant than a usable answer. – underscore_d Dec 02 '15 at 00:31