Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 633189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:09:28+00:00 2026-05-13T20:09:28+00:00

Just because I’ve never read binary files before I wrote a program that reads

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:09:29+00:00Added an answer on May 13, 2026 at 8:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As far as I know your own code is compiled… May 13, 2026 at 9:12 pm
  • Editorial Team
    Editorial Team added an answer As far as I know there is no way of… May 13, 2026 at 9:12 pm
  • Editorial Team
    Editorial Team added an answer You probably need nested routes. It automatically should handle relations.… May 13, 2026 at 9:12 pm

Related Questions

I want use html5's new tag to play a wav file (currently only supported
Just because I'm curious--is there any C analog to the functionality of the STL
I've just recently been using the EF just because I thought it was simplier
I've just set up exim on my ubuntu computer. At the moment it will
I've just wasted the past two hours of my life trying to create a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.