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

  • Home
  • SEARCH
  • 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 939285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:45:54+00:00 2026-05-15T21:45:54+00:00

I came a across a new problem when using templates. This is me being

  • 0

I came a across a new problem when using templates. This is me being a little creative from a book I recently read, expanding his ideas, and it has this code example.

Say you have a templated Array2D class. And you have this method (Array2D::WriteFile):

    bool WriteFile( const char* p_filename )
{
    FILE* outfile = 0;
    int written = 0;

    // open the file
    outfile = fopen( p_filename, "wb" );

    // return if it couldn't be opened
    if( outfile == 0 )
        return false;

    // write the array and close thef ile
    written = fwrite( m_array, sizeof( Datatype ), m_size, outfile );
    fclose( outfile );

    // if we didn't write the number of items we expected,
    // return failure
    if( written != m_size )
        return false;

    // return success.
    return true;
}

This works fine for basic data types such as int, char, float, etc. But what if you get a little creative making the data type a compound variable such as SLinkedList (singly linked list). It would be Array2D. Then you call your old WriteFile() function. It’ll save it, but you just only saved one node. The rest of the nodes went into oblivion never to return.

A few ideas came in my head:
1. Twiddle your thumbs watching the clock go by towards your deadline.
2. Make Array2D::WriteFile() a pure virtual function causing a more specific class to save it the way it should. But then I can’t use Array2D by itself in other stand-alone situations.
3. Write a function pointer to save, but I think it could get messy because you don’t know what data your passing into til the time comes. It could vary based on the data type.
4. Realize templates aren’t the solution, perhaps.

The template class isn’t solving every case scenario for me based on the data type. So what would be a good solution in your opinion? Thanks!

  • 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-15T21:45:55+00:00Added an answer on May 15, 2026 at 9:45 pm

    Your objects need to be serializable. Think about what actually happens under the hood when you try to write out the head of a linked list this way – of course it just writes the first node; the write function has no idea what it’s writing, so it has no idea how to follow the pointers to the next node and write that too. It just sees bits and writes them out until the end of the current object, which is in that case just the first node.

    Serialization basically means, for each non-trivial type, writing a method that packages up that type into a flat stream of bytes suitable for writing out (and ideally also a second method that reads that format back in).

    Now, your question is tagged C++, but the code you’ve written is very C-like. The normal way to implement a serializable object in C++ is to override the << operator with a std::ostream& argument and return value, e.g.

     std::ostream& MyType::operator<< (std::ostream& out)
     {
        // Here, write out the logical contents of this object in whatever format you
        // feel is appropriate (keep in mind endianness and floating point representation
        // if you want portability!)
        return out << this->field1 << this->field2 << this->field3;
     }
    

    And symmetrically

     std::istream& MyType::operator>> (std::istream& in)
     {
        // Here, read in the logical contents of this object in the same format
        return in >> this->field1 >> this->field2 >> this->field3;
     }
    

    Now, you can just do something like this:

     MyType t;
     std::ofstream outputFile("myoutputfile.dat");
    
     outputFile << t;
    

    Note that this approach of overriding the stream operators means that if, for example MyType::field2 happens to itself be a complex object, it will still be serialized and deserialized properly in the above code as long as its stream operators are overridden.

    But if you want to continue using C-style file I/O as in your original post, things aren’t going to be quite so clean. If your code is really supposed to be C++ though, you should be using the iostream libraries to do your file I/O.

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

Sidebar

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.