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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:00:11+00:00 2026-06-01T18:00:11+00:00

I’m trying to understand how to properly right data to a binary file. The

  • 0

I’m trying to understand how to properly right data to a binary file. The data is being written but when read back in for testing it is obvious I am not writing it properly. The data is read/written as follows.

Unsigned Char tells how long next string will be, then a string is written, and then an integer ID is written. ( or length studentname student_id ). The following is the printList function which saves the data to a file.

void printList(struct node * nptr, string filename){
  ofstream myfile;
  myfile.open( filename.c_str(), ios::binary );

  while(nptr){

  myfile.write( (char*) &nptr->data.len, sizeof( unsigned char ) );
  myfile.write( (char*) nptr->data.name, len * sizeof(char) );
  myfile.write( (char*) &nptr->data.id, sizeof(int) );

 //myfile.write( (const char*) &nptr->data, sizeof( Student ) );

 nptr = nptr->next;
}

myfile.close();

}

Here is the student struct from which the data is stored in a link list:

struct node{
Student data;
struct node* next;
};

//=== Student struct===//
 struct Student{
    unsigned char len;
char* name;
int id;
};
//====================//

Here is the loadbin file I wrote to read the binary back in, again I am messing something up and I have read and watched a ton of tutorials on this. I really am stumped.

unsigned char len;
char* name;
int id;

int main( int argc, char* argv[] )
{
    if( argc > 1 )
    {
        ifstream f( argv[1] , ios::binary);

        while( !f.eof() )
        {
            f.read( (char*) &len , sizeof( unsigned char ) );

            if( f.eof() )
                break;

        name = new char[len+1];
        name[len+1] = '\0';

        f.read( (char*) name , len * sizeof( char ) );
        f.read( (char*) &id , sizeof( int ) );

        cout << (int)len << " " << name << " " << id 
                                            << "\n";
        }
    }

   else
        cout << "\nToo few arguments.\n";

}

Some more info, I can’t change how the file is being read back in. The fix has to come from when I’m writing. I just included that in case it would 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-06-01T18:00:12+00:00Added an answer on June 1, 2026 at 6:00 pm

    For a start, I’m not even sure this should compile, unless you have a len variable floating around somewhere, such as in your last code segment:

    myfile.write( (char*) nptr->data.name, len * sizeof(char) ); // len, what len?
    

    Since sizeof(char) is always one and I prefer explicit expressions as much as possible, I’d rewrite the three output lines as:

    myfile.write ((const char *)(&(nptr->data.len)), 1);
    myfile.write ((const char *)(nptr->data.name), nptr->data.len);
    myfile.write ((const char *)(&(nptr->data.id)), sizeof(int));
    

    A full program showing how to use that follows, similar enough that it will help, but different enough so that you can’t just hand it in as your own work 🙂

    #include <iostream>
    #include <fstream>
    
    struct Student {
        unsigned char len;
        char *name;
        int id;
    };
    
    struct node {
        Student data;
        struct node *next;
    };
    
    int main (void) {
        // Create dummy list, two nodes.
    
        node *temp = new node();
        temp->data.len = 6;
        temp->data.name = (char *)"diablo";
        temp->data.id = 2;
        temp->next = NULL;
    
        node *shortList = new node();
        shortList->data.len = 3;
        shortList->data.name = (char *)"pax";
        shortList->data.id = 1;
        shortList->next = temp;
    
        // Write it out.
    
        std::ofstream myfile;
        myfile.open ("xyzzy.bin", std::ios::binary);
        node *curr = shortList;
        while (curr != NULL) {
            myfile.write ((const char *)(&(curr->data.len)), 1);
            myfile.write ((const char *)(curr->data.name), curr->data.len);
            myfile.write ((const char *)(&(curr->data.id)), sizeof(int));
            curr = curr->next;
        }
        myfile.close ();
    
        return 0;
    }
    

    If you run that program then do a hex dump, you get:

    addr  +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f  0123456789abcdef
    ----  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  ----------------
    0000  03 70 61 78 01 00 00 00 06 64 69 61 62 6c 6f 02  .pax.....diablo.
    0010  00 00 00                                         ...
    0013
    

    which I think matches the format you wanted.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I want to construct a data frame in an Rcpp function, but when I
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
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.