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

  • Home
  • SEARCH
  • 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 985129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:07:17+00:00 2026-05-16T05:07:17+00:00

How can I pack and write a struct to a file using C so

  • 0

How can I “pack” and “write” a struct to a file using C so that:

struct a {
    uint64_t a;
    char* b;
    uint16_t c;
} a;
a b;
b.a = 3;
b.b = "Hello";
b.c = 4;

gets written to the file as

00 00 00 00 00 00 00 03 48 65 6c 6c 6f 00 00 04
  • 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-16T05:07:18+00:00Added an answer on May 16, 2026 at 5:07 am

    In C, you’ll have to code a function to do this for you. You can’t just blat the structure out to disk because b is a pointer that makes no sense without the backing string. And, unless you know (and can control) how your compiler packs its structures, you’re better off with a utility function anyway, even without pointers.

    And, as if that wasn’t enough, you should output the length of the string as well so you know how many bytes to read back.

    You’ll be looking for something like:

    int better_than_blat (FILE *f, struct a *x) {
        size_t len = strlen (x->b);
        if (fwrite (&(x->a), sizeof(long), 1, f) != 1) return -1;
        if (fwrite (&len, sizeof(size_t), 1, f) != 1) return -1;
        if (fwrite (x->b, len, 1, f) != 1) return -1;
        if (fwrite (&(x->c), sizeof(short), 1, f) != 1) return -1;
        return 0;
    }
    
    int better_than_unblat (FILE *f, struct a *x) {
        size_t len;
        if (fread (&(x->a), sizeof(long), 1, f) != 1) return -1;
        if (fread (&len, sizeof(size_t), 1, f) != 1) return -1;
        x->b = malloc (len + 1);
        if (x->b == NULL) return -1;
        memset (x->b, 0, len + 1);
        if (fread (x->b, len, 1, f) != 1) return -1;
        if (fread (&(x->c), sizeof(short), 1, f) != 1) return -1;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP, a simple read and write file can be done by using fread()
Using the HTML Agility Pack , how can I remove all HTML attributes, elements,
I'm trying to write an app using GtkClutter but I can't get the actors
Following piece of code is from zipfile.py. self.fp.write(zinfo.FileHeader()) def FileHeader(self): header = struct.pack(structFileHeader, stringFileHeader,
I'm using the FireFox API to write to a binary file from my JavaScript
Can Html Agility Pack be used to parse an html string fragment? Such As:
Can I use Html Agility Pack to make the output look nicely indented, unnecessary
How can I do this Perl code in PHP? print unpack (H*, pack (B*,
If this is possible, one can index into a variadic template parameter pack without
Can I authenticate with just Google account username and password instead of using OAuth?

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.