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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:41:36+00:00 2026-06-18T15:41:36+00:00

I am trying to retrieve some data from a binary file to put them

  • 0

I am trying to retrieve some data from a binary file to put them in a linked list, here’s my code to write to the file:

void Pila::memorizzafile()
{
     int contatore = 0; 
     puntarec temp = puntatesta;
     ofstream miofile;
     miofile.open("data.dat" , ios::binary | ios::out);
     if(!miofile) cerr << "errore";
     else
     {
                while(temp)
                {
                            temp->elem.writes(miofile);
                            contatore++;
                            temp = temp->next;
                }
                //I go back at the beginning of the file to write how many elements I have
                miofile.seekp(0, ios::beg);
                miofile.write((const char *)&contatore , sizeof(int));
                miofile.close();
     }
}

And the function writes:

void Fiche::writes(ofstream &miofile)
{
     //Valore.
     miofile.write((const char *)&Valore,sizeof(int));
     //Materiale, I write the dimension of the string.
     int buff = strlen(Materiale);
     miofile.write((const char *)&buff,sizeof(int));
     //Writing the string
     miofile.write(Materiale,buff*sizeof(char));
     //Dimension of Forma
     buff = strlen(Forma);
     miofile.write((const char*)&buff,sizeof(int));
     //The string itself
     miofile.write(Forma,buff*sizeof(char));
     //Dimension of Colore.
     buff = strlen(Colore);
     miofile.write((const char*)&buff,sizeof(int));
     //The string
     miofile.write(Colore,buff*sizeof(char));
}

Now for the reading part, I am trying to make a constructor which should be able to read directly from the file, here it is:

Pila::Pila(char * nomefile)
{
     puntatesta = 0;
     int contatore = 0;
     ifstream miofile;
     miofile.open(nomefile  , ios::binary | ios::in);
     if(!miofile) cerr << "errore";
     else
     {
         //I read how many records are stored in the file
         miofile.read((char*)&contatore,sizeof(int));
         Fish temp;
         for(int i = 0; i < contatore; i++)
         {
                 temp.reads(miofile);
                 push(temp);
         }            
         miofile.close();
     }
}

And the reading function:

void Fiche::reads(ifstream &miofile)
{
     //I read the Valore
     miofile.read((char*)&Valore,sizeof(int));
     //I create a temporary char *

     char * buffer;
     int dim = 0;
     //I read how long will be the string
     miofile.read((char*)&dim,sizeof(int));
     buffer = new char[dim];
     miofile.read(buffer,dim);
     //I use the set function I created to copy the buffer to the actual member char*
     setMateriale(buffer);
     delete [] buffer;
     //Now it pretty much repeats itself for the other stuff
     miofile.read((char*)&dim,sizeof(int));
     buffer = new char[dim];
     miofile.read(buffer,dim);
     setForma(buffer);
     delete [] buffer;
     //And again.
     miofile.read((char*)&dim,sizeof(int));
     buffer = new char[dim];
     miofile.read(buffer,dim);
     setColore(buffer);
     delete [] buffer;    
}

The code doesn’t give me any error, but on the screen I read random characters and not even remotely close to what I wrote on my file. Anyone could help me out, please?

EDIT:

As requested here’s an example of input&output:

Fiche A("boh" , 4 , "no" , "gaia");
Fiche B("Marasco" , 3 , "boh" , "nonnt");
Fiche C("Valori" , 6 , "asd" , "hey");
Fiche D("TipO" , 7 , "lol" , "nonloso");
Pila pila;
pila.push(A);
pila.push(B);
pila.push(C);
pila.push(D);
pila.stampa();
pila.memorizzafile();

And:

Pila pila("data.dat");
pila.stampa();
  • 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-18T15:41:39+00:00Added an answer on June 18, 2026 at 3:41 pm

    This is probably your error:

                //I go back at the beginning of the file to write how many elements I have
                miofile.seekp(0, ios::beg);
                miofile.write((const char *)&contatore , sizeof(int));
                miofile.close();
    

    By seeking to the beginning and then writing. You are overwriting part of the first object.

    I think your best bet is to run through the list and count the elements first. Write this then proceed to write all the elements. It will probably be faster anyway (but you can time it to make sure).

    I think you are using way to many C structures to hold things.

    Also I would advice against a binary format unless you are saving huge amounts of information. A text format (for your data) is probably going to be just as good and will be human readable so you can look at the file and see what is wrong.

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

Sidebar

Related Questions

trying to retrieve some data from the database and put in into my form.
im trying to retrieve data from my table only for some reason I receive
I have been trying to think of a way to retrieve some data from
I'm trying to retrieve some data from a JSON response, but this doesn't seem
I am trying to use LINQ to retrieve some data from a dictionary. var
I'm trying to get some data from a PHP file which only gives a
I am trying to retrieve data from a table from some old legacy work,
Im trying to retrieve some data from JSON object which holds location information such
I am trying to retrieve the text data from an ePub file using Java.
Just trying to get some perspective about how to retrieve data from umbraco via

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.