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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:03:05+00:00 2026-06-13T11:03:05+00:00

I have a vector: class Element { public: string pathName; ui64 offsitePtr; ui64 subPage;

  • 0

I have a vector:

class Element
{
public:

    string pathName;
    ui64 offsitePtr;
    ui64 subPage;

public:

    Element(void);

    ~Element(void);
};

vector<Element> elem;

The size of elem will be controlled less than 4096 bytes. At the end of the program, I should fwrite elem into a binary file. The solution I’m using currently is to make a char buffer and write the element in elem in it. I don’t think it is a good idea. Is there any other good ideas?

  • 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-13T11:03:06+00:00Added an answer on June 13, 2026 at 11:03 am

    Provided you don’t write the vector or the Elements directly from memory, it’s okay. You’ll need to serialize anything that isn’t POD (plain old data). That is in your case: vector and string.

    The vector is easy, because you can just make a function for Element. But you probably want to serialize the vector size:

    ofstream& WriteVec( ofstream& s, vector<Element> &elem )
    {
        size_t size = elem.size();
        s.write( (char*)&size, sizeof(size) );
        for( int i = 0; i < size; i++ )
            elem(i).Write(s);
        return s;
    }
    

    For your element:

    ofstream& Element::Write( ofstream& s )
    {
        // Serialize pathName
        size_t strsize = pathName.size();
        s.write( (char*)&strsize, sizeof(strsize) );
        s.write( pathName.c_str(), strsize );
    
        // Serialize other stuff
        s.write( (char*)&offsitePtr, sizeof(offsitePtr) );
        s.write( (char*)&subPage, sizeof(subPage) );
    }
    

    And you do something similar when you read. Will leave you to work that out =) Note in each case, you’re writing a size, and then the data. When you read, you read in the size, then resize your structure before reading the contents into it.

    Oh, and make sure you open the file stream in binary mode.

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

Sidebar

Related Questions

I have a std::vector of some class of the form class A{ public: A():i(someNumber){}
I have class A: public B { ...} vector<A*> v; I want to do
Suppose I have a class similar to the following: #include <vector> class element {
I have a class exactly like this: public class Operator { private String oper;
I have a class like this: template<class T> class AdjacencyList { public: void delete_node(const
I have a class which has a string vector as a variable and a
I have a vector class and I defined the __mul__ method to multiply a
I have a vector of pointers to class objects. These class objects invoke new
I have a vector of pointers to a class. I need to call their
I have a vector called players and a class called Player. And what I'm

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.