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

The Archive Base Latest Questions

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

Problem: I need to write/read objects from a file.This because I need to write/read

  • 0

Problem: I need to write/read objects from a file.This because I need to write/read a std::list to file, but in whatever case.Not only with T=int (this would be simple), but with whatever parameter.
In Java with OutputFileStream and InputFileStream this was possibile, but I suppose it’s just a JVM feature.
However I am trying to read/write objects to a file:

template <class T>
bool write_object(std::fstream& out, const T& object)
{
    bool result=false;
    char* ptr;
    const unsigned long size=sizeof(object);
    if(out.good())
    {
        result=true;
        ptr=(char*)&object;
        for(unsigned int i=0;i<size;i++)
        {
            out << *ptr;
            ptr++;
        }
    }
    return result;
}

template <class T>
bool read_object(std::fstream& in, T& object)
{
    bool result=false;
    T* ptr;
    T swap_temp;
    const unsigned long size=sizeof(object);
    char temp[size];
    std::streampos pos;
    if(in.good())
    {
        pos=in.tellg();
        if(pos!=-1)
        {
            result=true;
            for(unsigned long i=0; i<size; i++)
            {
                if(!in.good())
                {
                    result=false;
                    break;
                }
                else
                {
                    in >> temp[i];
                }
            }
        }
    }
    if(result==false)
    {
        in.seekg(pos);
    }
    else
    {
        ptr=(T*)temp;
        swap_temp=*ptr;
        object=swap_temp;
    }
    return result;
}

But I have encountered the following problems:
-sizeof operator just returns the size of all fields, it does not consider also the data pointed by internal fields;
-If in the class there is a pointer, then this pointer could point to a “wrong” memory address, (e.g.) if I use a pointer which points to a C-style string in memory, once the program ends the string is deallocated.When the instance of the program runs again,this area of memory could be anywhere.

This method is wrong because for example sizeof(string) with my compiler returns 4.
So I suppose it uses a char pointer (I am on a 32-bit machine) to point to the C-style string allocated.Probably it does not even keep trace of the length.
So if the string has 32 characters I don’t notice it, it just copies the value of the pointer.

  • 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-01T03:00:46+00:00Added an answer on June 1, 2026 at 3:00 am

    Your approach can’t work since C++ doesn’t know java-like techniques like reflection so you can’t distinguish between pointers and other members.

    What you want is called serialisazion and you can use it with libraries like Boost.Serialization (Demo).

    But even then, you can’t write a general function, you have to define it specifically for each object.

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

Sidebar

Related Questions

I have a problem. I need to trace all read/write operations to the registry
This is a CSS problem. I need to trim the white spaces from a
Im working on a java project where i need to read some objects from
Ok, so here's the problem I have to solve. I need to write a
The Problem: I need to receive different types of content from a number of
An XSLT-newbie problem: I need to substitute a text value in XML-file. All other
This is my problem: I need to store a lot of log messages and
I'm using the jackson library for serializing/deserializing to/from JSON. I need that this JSON
To solve some problem I need to compute a variant of the pascal's triangle
I faced a problem - I need to use a macro value both as

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.