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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:30:52+00:00 2026-05-16T10:30:52+00:00

I have an array of a large structure that I am trying to output

  • 0

I have an array of a large structure that I am trying to output to hard drive. I seem to be able to write to hard drive just fine (though it’s difficult to verify by looking at the binary data), however when I try to read it back, I always end up with a garbled mess. Any ideas what I’m doing wrong?

here’s the structure configuration:

class xyz
{
public:
    double x, y, z;
};
class trianglePackage
{
public:

    int score;
    int position;

    xyz contactCoordinates;
    xyz normalVector;
    xyz locatorOffset;

};
class quadanglesOutput
{
public:

    int locator1position, locator2position, locator3position, locator4position;

    xyz centroid;

    int surfaceAreaScore;
    int centroidDifferance1Score;
    int centroidDifferance2Score;
    int minDistance1Score;
    int minDistance2Score;

    int totalLocatorScore;
    int totalHullScore;
    int totalScore;

    double surfaceArea;
    double centroidDifferance1;
    double centroidDifferance2;
    double minDistance1;
    double minDistance2;

    int hull;

    trianglePackage locator1, locator2, locator3, locator4;
};

and here are the read/write functions I’m using:

void outputQuadangleOutput(quadanglesOutput* output, string description, param parameters)
{

    string outputName = parameters.fileName + " " + description + ".bin";
    cout << "Output " << outputName.c_str() << "...";
    ofstream output2;
    output2.open(outputName.c_str());
    output2.write(reinterpret_cast<char*>(output), streamsize(parameters.topXlist * sizeof(quadanglesOutput)));
    output2.close();
    cout << "done" << endl;

}
void readIn(quadanglesOutput* pointer, param parameters, string description)
{
    string fileName = parameters.fileName + " " + description + ".bin";
    cout << "openining " << fileName << "...";
    ifstream readFile;
    readFile.open(fileName.c_str());
    readFile.read(reinterpret_cast<char*>(pointer), (parameters.topXlist * sizeof(quadanglesOutput)));
    readFile.close();
    cout << "done" << endl;
}

Typically the arrays of structures are about 100 in length, but usually only about the first 25 read back correctly, everything else is default uninitialized data.

I’m 99% sure that it’s something wrong with my code, however is there a possibility it has something to do with four byte alignment?

Thanks.

  • 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-16T10:30:52+00:00Added an answer on May 16, 2026 at 10:30 am

    It may be an issue with byte alignment, use pragma.
    try wrap classes around with

    #PRAGMA PACK PUSH(1)
    ....
    #PRAGMA PACK POP
    

    or

    #PRAGMA PACK(1)
    struct{
    ..
    }
    

    Try those as well:
    Force binary flag for the stream.

    ios_base::binary

    readFile.open(fileName.c_str(), ios_base::binary);
    

    Try to flush the the stream.

    stream.write(...)
    stream.flush()
    

    //i know that close() should flush it.

    UPDATE:
    Everything works for me:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>
    
    using namespace std;
    #pragma pack(1)
    class xyz
    {
    public:
        double x, y, z;
    };
    
    #pragma pack(1)
    class trianglePackage
    {
    public:
    
        int score;
        int position;
    
        xyz contactCoordinates;
        xyz normalVector;
        xyz locatorOffset;
    
    };
    
    #pragma pack(1)
    class quadanglesOutput
    {
    public:
    
        int locator1position, locator2position, locator3position, locator4position;
    
        xyz centroid;
    
        int surfaceAreaScore;
        int centroidDifferance1Score;
        int centroidDifferance2Score;
        int minDistance1Score;
        int minDistance2Score;
    
        int totalLocatorScore;
        int totalHullScore;
        int totalScore;
    
        double surfaceArea;
        double centroidDifferance1;
        double centroidDifferance2;
        double minDistance1;
        double minDistance2;
    
        int hull;
    
        trianglePackage locator1, locator2, locator3, locator4;
    };
    
    class param
    {
    public:
        string fileName;
        int topXlist;
    };
    
    
    void outputQuadangleOutput(quadanglesOutput* output, string description, param parameters)
    {
    
        string outputName = parameters.fileName + " " + description + ".bin";
        cout << "Output " << outputName.c_str() << "...";
        ofstream output2;
        output2.open(outputName.c_str());
        output2.write(reinterpret_cast<char*>(output), streamsize(parameters.topXlist * sizeof(quadanglesOutput)));
        output2.close();
        cout << "done" << endl;
    
    }
    void readIn(quadanglesOutput* pointer, param parameters, string description)
    {
        string fileName = parameters.fileName + " " + description + ".bin";
        cout << "openining " << fileName << "...";
        ifstream readFile;
        readFile.open(fileName.c_str());
        readFile.read(reinterpret_cast<char*>(pointer), (parameters.topXlist * sizeof(quadanglesOutput)));
        readFile.close();
        cout << "done" << endl;
    }
    
    
    
    int main(int argc, char *argv[])
    {
    
        quadanglesOutput a = {0};
        cout<<"total score:"<<a.totalScore<<endl;
        cout<<"locator position:"<<a.totalScore<<endl;
        cout<<"locator position:"<<a.locator1.position<<endl;
        cout<<"locator position:"<<a.locator2.normalVector.y <<endl;
        cout<<"sizeof quadangsomething:"<<sizeof(quadanglesOutput)<<endl;
        a.totalScore=1;
        a.locator1.position=333445;
        a.locator2.normalVector.y = 999.3224;
        cout<<"total score:"<<a.totalScore<<endl;
        cout<<"locator position:"<<a.locator1.position<<endl;
        cout<<"locator position:"<<a.locator2.normalVector.y <<endl;
        param p = {"C:/", 1};
        outputQuadangleOutput(&a, "file1", p);
    
        quadanglesOutput *b = new quadanglesOutput();
        readIn(b, p, "file1");
        cout<<"new total score:"<<b->totalScore<<endl;
        cout<<"new locator position:"<<b->locator1.position<<endl;
        cout<<"new locator position:"<<b->locator2.normalVector.y <<endl;
    
        delete b;
    
    
    
    
    
        string asdf;
        cin>>asdf;
    
    };
    

    OUTPUT:
    total score:0
    locator position:0
    locator2.normalVector.y :0
    sizeof quadangsomething:436
    total score:1
    locator position:333445
    locator2.normalVector.y :999.322
    Output C:/ file1.bin…done
    openining C:/ file1.bin…done
    new total score:1
    new locator position:333445
    new locator2.normalVector.y :999.322

    without pragma it’s still correct but you can see the difference in size:

    sizeof quadangsomething:440

    But packing it is good when sending structures over network.
    Because here system alligns it always in the same fashion.

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

Sidebar

Related Questions

I'm trying to put some folders on my hard-drive into an array. For instance,
I have a large array with around 20k objects in it. Each object has
I have an application that is pretty memory hungry. It holds a large amount
I'm working with arrays of image filepaths. A typical array might have 5 image
I'm struggling to create a generic (or untyped) array in C (I'm aware that
I'm trying to create a function which takes an array as an argument, adds
I'm trying to parse a moderately large XML file (6mb) in php using simpleXML.
I have a small C# library with a static class that holds a very
I have an array of documents, where each document have another simple one-dimensional array
I have an array of key values {val1: [{gender:male}, {age:23}, {favorite-color:red}] } The thing

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.