DaC*_*own 8 c++ binary casting
仅仅因为我在编写读取二进制STL文件的程序之前从未读过二进制文件.我使用ifstream带有char*参数的读取成员.要将我的结构转换为char*,我使用reinterpret_cast.但据我记得每本关于C++的书我读到的都说"不要使用reinterpret_cast,除非你必须".什么是更好的方式读取二进制数据,不一定是直接的,但最后到一个结构,没有reinterpret_cast?
主要功能:
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
和三角形结构
//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)
(额外的问题:#pragma pack(1)不适用于cygwins g ++ - 4.如何确定结构的大小?)
好吧,那段代码看起来不错.你甚至关心填充问题.我看不出你怎么能避免在这里施展.你可以这样做:
static_cast<char*>(static_cast<void*>(t))
但实际上,我不会在我的代码中这样做.这只是一种更直接的reinterpret_cast的嘈杂方式char*.(请参阅通过void*而不是使用reinterpret_cast进行转换).
结构大小可以使用确定sizeof.你只需要static在类中初始化成员.cpp(但是,编译器不再知道它的值,::size也不能内联它).
或者,您可以将其编写为静态内联成员函数.在它的主体中,类类型被认为是完整的并且sizeof (triangle)是允许的.或者您可以sizeof像在注释中一样使用,但是使用类型而不是成员(指的是仅在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
};
但是,第二种方式并不好,因为您在添加成员时很容易忘记更新它.