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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:21:13+00:00 2026-06-14T20:21:13+00:00

I’ve finally figured out how to write some specifically formatted information to a binary

  • 0

I’ve finally figured out how to write some specifically formatted information to a binary file, but now my problem is reading it back and building it back the way it originally was.

Here is my function to write the data:

void save_disk(disk aDisk)
{
    ofstream myfile("disk01", ios::out | ios::binary);
    int32_t entries;
    entries = (int32_t) aDisk.current_file.size();
    char buffer[10];
    sprintf(buffer, "%d",entries);

    myfile.write(buffer, sizeof(int32_t));

    std::for_each(aDisk.current_file.begin(), aDisk.current_file.end(), [&] (const file_node& aFile)
    {
        myfile.write(aFile.name, MAX_FILE_NAME);
        myfile.write(aFile.data, BLOCK_SIZE - MAX_FILE_NAME);
    });

}

and my structure that it originally was created with and what I want to load it back into is composed as follows.

struct file_node
{
    char  name[MAX_FILE_NAME];
    char  data[BLOCK_SIZE - MAX_FILE_NAME];

    file_node(){};
};

struct disk
{
    vector<file_node> current_file;
};

I don’t really know how to read it back in so that it is arranged the same way, but here is my pathetic attempt anyway (I just tried to reverse what I did for saving):

void load_disk(disk aDisk)
{
    ifstream myFile("disk01", ios::in | ios::binary);
    char buffer[10];

    myFile.read(buffer, sizeof(int32_t));

    std::for_each(aDisk.current_file.begin(), aDisk.current_file.end(), [&] (file_node& aFile)
    {
        myFile.read(aFile.name, MAX_FILE_NAME);
        myFile.read(aFile.data, BLOCK_SIZE - MAX_FILE_NAME);
    });

}

^^ This is absolutely wrong. ^^

I understand the basic operations of the ifstream, but really all I know how to do with it is read in a file of text, anything more complicated than that I’m kind of lost.

Any suggestions on how I can read this in?

  • 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-06-14T20:21:14+00:00Added an answer on June 14, 2026 at 8:21 pm

    You’re very close. You need to write and read the length as binary.

    This part of your length-write is wrong:

    char buffer[10];
    sprintf(buffer, "%d",entries);
    myfile.write(buffer, sizeof(int32_t));
    

    It only writes the first four bytes of whatever the length is, but the length is character data from a sprintf() call. You need to write this as a binary-value of entries (the integer):

    // writing your entry count.
    uint32_t entries = (uint32_t)aDisk.current_file.size();
    entries = htonl(entries);
    myfile.write((char*)&entries, sizeof(entries));
    

    Then on the read:

    // reading the entry count
    uint32_t entries = 0;
    myFile.read((char*)&entries, sizeof(entries));
    entries = ntohl(entries);
    
    // Use this to resize your vector; for_each has places to stuff data now.
    aDisk.current_file.resize(entries);
    std::for_each(aDisk.current_file.begin(), aDisk.current_file.end(), [&] (file_node& aFile)
    {
        myFile.read(aFile.name, MAX_FILE_NAME);
        myFile.read(aFile.data, BLOCK_SIZE - MAX_FILE_NAME);
    });
    

    Or something like that.

    Note 1: this does NO error checking nor does it account for portability for potentially different endian-ness on different host machines (a big-endian machine writing the file, a little endian machine reading it). Thats probably ok for your needs, but you should at least be aware of it.

    Note 2: Pass your input disk parameter to load_disk() by reference:

    void load_disk(disk& aDisk)
    

    EDIT Cleaning file_node content on construction

    struct file_node
    {
        char  name[MAX_FILE_NAME];
        char  data[BLOCK_SIZE - MAX_FILE_NAME];
    
        file_node()
        { 
            memset(name, 0, sizeof(name));
            memset(data, 0, sizeof(data));
        }
    };
    

    If you are using a compliant C++11 compiler:

    struct file_node
    {
        char  name[MAX_FILE_NAME];
        char  data[BLOCK_SIZE - MAX_FILE_NAME];
    
        file_node() : name(), data() {}
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a French site that I want to parse, but am running into

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.