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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:31:57+00:00 2026-05-18T20:31:57+00:00

I’ve got these two structs that I need to store in files. I can’t

  • 0

I’ve got these two structs that I need to store in files. I can’t find a good way to get them into a file without parsing the struct and writing it as a text file, which is far less efficient than I’d like. What is the simplest way to get a struct of mostly primitives into a file? The structs are below

struct object
{
    unsigned short id;
    float x;
    float y;
    unsigned short type;
    unsigned char flags;
};

struct sector
{
    unsigned long id;
    unsigned long neighbors[4]; // North,East,South,West
    std::vector<object> objects;
};

I’ve tried the code below to write the struct to a file, but it doesn’t work very well. The size of the file after execution is only 4.7kb, while it should be much larger than that.

sector s;
object o;
s.id = 1;
s.neighbors = {2, 3, 4, 5};
o.id = 1; o.x = 2.0f; o.y = 3.0f; o.type = 4; o.flags = 6;
for(int i = 0; i < 65536; i++)
{
    s.objects.push_back(o);
    o.id += 1; o.x += 1.0f; o.y += 1.0f; o.type += 1; o.flags += 1;
}

ofstream os("world.dat", ios::binary|ios::out);
s.objects.resize(s.objects.size());
int size = s.objects.size() * sizeof(object);
os.write((char*)&size, sizeof(int));
os.write((char*)&s, size);
os.close();

Any suggestions?

  • 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-18T20:31:58+00:00Added an answer on May 18, 2026 at 8:31 pm

    You can’t write a struct containing a vector using raw byte I/O. The vector class is a small data structure that stores its data somewhere on the heap, not in the struct. This is because it is impossible for a struct to “know,” ahead of time, how many objects it will hold.

    You can write the contents of the vector thus:

    os.write((const char*)&s.id, sizeof(s.id));
    os.write((const char*)&s.neighbors, sizeof(s.neighbors));
    os.write((const char*)&s.objects[0], size);
    

    As to why the file ends up with only 4.7 kB in it, I don’t know. Print out size to see how much data is being written. I checked the code after the above changes, and it works.

    Also, you don’t need s.objects.resize(s.objects.size());. Resizing the vector to its own size is redundant.

    Also note a small quirk. sizeof(object) == 16 on my architecture, which is more than required for the above struct (2 + 4 + 4 + 2 + 1 == 13). The compiler usually adds padding to ensure that primitives of size 2n are aligned to a 2n-byte boundary, which results in holes inside each struct and between structs in the vector.

    The consequence of this is that you are writing extra bytes to the file, some of which are random noise. In non-debug builds, those holes are never filled with anything, so they just contain whatever rubbish was at that memory location before it was allocated for your purposes. Therefore, if you run the program twice, it is possible that the two files will not be identical.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are
I want to count how many characters a certain string has in PHP, but

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.