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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:33:43+00:00 2026-05-12T19:33:43+00:00

I must write array of struct Data to hard disk: struct Data { char

  • 0

I must write array of struct Data to hard disk:


struct Data {
  char cmember;
  /* padding bytes */
  int  imember;  
};


AFAIK, most of compilers will add some padding bytes between cmember and imember members of Data, but I want save to file only actual data (without paddings).
I have next code for saving Datas array (in buffer instead of file for simplification):


bool saveData(Data* data, int dataLen, char* targetBuff, int buffLen)
{
  int actualLen = sizeof(char) + sizeof(int); // this code force us to know internal
                                              // representation of Data structure
  int actualTotalLen = dataLen * actualLen; 
  if(actualTotalLen > buffLen) {
    return false;
  }

  for(int i = 0; i < dataLen; i++) {
    memcpy(targetBuff, &data[i].cmember, sizeof(char));
    targetBuff += sizeof(char);
    memcpy(targetBuff, &data[i].imember, sizeof(int));
    targetBuff += sizeof(int);
  }
  return true;
}


As you can see, I calculate actual size of Data struct with the code: int actualLen = sizeof(char) + sizeof(int). Is there any alternative to this ? (something like int actualLen = actualSizeof(Data))

P.S. this is synthetic example, but I think you understand idea of my question...

  • 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-12T19:33:44+00:00Added an answer on May 12, 2026 at 7:33 pm

    Just save each member of the struct one at a time. If you overload << to write a variable to a file, you can have

    myfile << mystruct.member1 << mystruct.member2;
    

    Then you could even overload << to take an entire struct, and do that inside the struct’s operator<<, so in the end you have:

    myfile << mystruct;
    

    Resulting in save code that looks like:

    myfile << count;
    for (int i = 0; i < count; ++i)
        myFile << data[i];
    

    IMO all that fiddling about with memory addresses and memcpy is too much of a headache when you could do it this way. This general technique is called serialization – hit google for more, it’s a well-developed area.

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

Sidebar

Related Questions

This is my code: #include <stdio.h> #include <stdlib.h> int main() { char c, *data,
What I must write in my .emacs file so that the *scratch* buffer is
I need to write a generic class where the type parameter must be something
If we must connect the db (redis)every time we need to write to or
i write row click handler: $('#mTable tr').unbind().live('click', function() { //specialFunction() }); specialFunction must not
I'm kinda stuck with something which must be appalingly simple. I'm trying to write
I need to write raw bytes to the file. I do it with: (.write
I'm trying to write an application which will populate an array with random numbers.
My task is to write the main function which defines an array of objects
I would like to write a function that generate an array of tuples containing

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.