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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:10:32+00:00 2026-05-23T10:10:32+00:00

I have a binary file where I log the offset at which writes to

  • 0

I have a binary file where I log the offset at which writes to other files occur and the data that is written at that offset. Now I define the structure of each log record as

struct log_record{
                    int offset;
                    char *data;
               }
struct log_record t;

When I write the record into the file, I have the length of data that I write into the file and hence before assigning I allocate space as

t.data = (char*)malloc(sizeof(char)*strlen(buff));/*buf holds the data to be written*/

Now I have the length of the record I am writing into the file…But the issue is while reading. How do I allocate space against a structure that I read the record into and in the fread what should be the size of the record. Am a few confused.
Would be gratefull for help

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

    You need to write the length of the string as the length of string is variable.

    Note that strlen() doesn’t return the size including the terminating NULL.

    EDIT + EDIT 2 (thanks to mu is too short) + EDIT 3 (thanks to mu is too short)

    This is how I’d do it:

    t.data = (char*) malloc(sizeof(char) * strlen(buff) + 1);
    strcpy(t.data, buff);
    
    // ...
    int data_size = strlen(t.data) + 1;
    fwrite(&t.offset,  1, sizeof(t.offset),   file);
    fwrite(&data_size, 1, sizeof(data_size),  file);
    fwrite(t.data,     1, strlen(t.data) + 1, file);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have binary data in a file that I can read into a byte
I have an app that loads records from a binary log file and displays
I have a binary log file with streaming data from a sensor (Int16). Every
I have a binary file that I have to parse and I'm using Python.
I have written a database application using a binary file as storage. it is
i am reading the bytes from binary file and now i have to get
I have a binary file that I need to insert a header at the
I have some binary files in Tomcat webapp folder, that I wish user to
Assuming that I have to write to a binary file. I can use the
I have this data file, which is supposed to be a normal ASCII file.

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.