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 537303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:54:46+00:00 2026-05-13T09:54:46+00:00

Every time i try to read a file form the hard drive and cast

  • 0

Every time i try to read a file form the hard drive and cast the data into a structure, i end up with problems of the data not casting properly. Is there a requirement with the reinterpret_cast() function that requires the number of bytes in a structure be a multiple of 4 bytes? If not, what am I doing wrong? If so, how do i get around that?

my structure looks like this: (they are in 50 byte chunks)

class stlFormat
{
public:

    float normalX, normalY, normalZ;
    float x1,y1,z1;
    float x2,y2,z2;
    float x3,y3,z3;

    char byte1, byte2;
};

Rest of my code:

void main()
{

int size;
int numTriangles;

int * header = new int [21]; // size of header

ifstream stlFile ("tetrahedron binary.STL", ios::in|ios::binary|ios::ate);

size = stlFile.tellg(); // get the size of file

stlFile.seekg(0, ios::beg); //read the number of triangles in the file
stlFile.read(reinterpret_cast<char*>(header), 84);

numTriangles = header[20];

stlFormat * triangles = new stlFormat [numTriangles]; //create data array to hold vertex data

stlFile.seekg (84, ios::beg); //read vertex data and put them into data array
stlFile.read(reinterpret_cast<char*>(triangles), (numTriangles * 50));

cout << "number of triangles: " << numTriangles << endl << endl;

for (int i = 0; i < numTriangles; i++)
{
    cout << "triangle " << i + 1 << endl;
    cout << triangles[i].normalX << " " << triangles[i].normalY << " " << triangles[i].normalZ << endl;
    cout << triangles[i].x1 << " " << triangles[i].y1 << " " << triangles[i].z1 << endl;
    cout << triangles[i].x2 << " " << triangles[i].y2 << " " << triangles[i].z2 << endl;
    cout << triangles[i].x3 << " " << triangles[i].z3 << " " << triangles[i].z3 << endl << endl;  
}

stlFile.close();
getchar();
}

Just for you John, although its rather incomprehensible. Its in hex format.

73 6f 6c 69 64 20 50 61 72 74 33 20 20 20 20 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
04 00 00 00 ec 05 51 bf ab aa aa 3e ef 5b f1 be
00 00 00 00 00 00 00 00 f3 f9 2f 42 33 33 cb 41
80 e9 25 42 9a a2 ea 41 33 33 cb 41 00 00 00 00
00 00 00 00 00 00 00 00 00 00 ab aa aa 3e ef 5b
71 3f 33 33 4b 42 00 00 00 00 f3 f9 2f 42 33 33
cb 41 80 e9 25 42 9a a2 ea 41 00 00 00 00 00 00
00 00 f3 f9 2f 42 00 00 ec 05 51 3f ab aa aa 3e
ef 5b f1 be 33 33 cb 41 00 00 00 00 00 00 00 00
33 33 cb 41 80 e9 25 42 9a a2 ea 41 33 33 4b 42
00 00 00 00 f3 f9 2f 42 00 00 00 00 00 00 00 00
80 bf 00 00 00 00 33 33 cb 41 00 00 00 00 00 00
00 00 33 33 4b 42 00 00 00 00 f3 f9 2f 42 00 00
00 00 00 00 00 00 f3 f9 2f 42 00 00

  • 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-13T09:54:46+00:00Added an answer on May 13, 2026 at 9:54 am

    Most likely, float has an alignment of four bytes on your system. This means that, because you use it in your structure, the compiler will make sure the start of the structure when allocated using normal methods will always be a multiple of four bytes. Since the raw size of your structure is 4*12+2 = 50 bytes, it needs to be rounded up to the next multiple of four bytes – otherwise, the second element of arrays of this structure would be unaligned. So your struct ends up 52 bytes, throwing off your parsing.

    If you need to parse a binary format, it’s often a good idea to either use compiler-specific directives to disable alignment, or read one field at a time, to avoid these problems.

    For example, on MSVC++, you can use __declspec(align(1)) Edit: Actually __declspec(align(X)) can only increase alignment restrictions. Oops. You’ll need to either load one field at a time, or make the padding part of the binary format.

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

Sidebar

Related Questions

I am trying to read in a file and every time , year is
If my file stream is in use (every time I try to debug, it
Every time try to set the value of any variable in my model object,
Every time I try to access my localhost/phpmyadmin it gives me this error --->
Every time I try to post to twitter in my rails app, the first
For some reason every time I try to count the number of rows in
I am getting a conversion error every time I try to submit a date
Started recently, basically every time I try to fetch or commit it gives me
I have one particular FLA that is crashing every time I try to compile
I'm using VS2008 (with windows XP). Every time I try to run a unit

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.