0

My problem goes like this: I have a class called 'Register'. It has a string attribute called 'trainName' and its setter:

class Register {

 private:
    string trainName;

 public:
    string getTrainName();
};

As a matter of fact, it is longer but I want to make this simpler.

In other class, I copy several Register objects into a binary file, previously setting trainName.

Register auxRegister = Register();   
auxRegister.setName("name");

for(int i = 0; i < 10; i++) {

  file.write(reinterpret_cast<char*>(&auxRegister),sizeof(Register));

}

Later on, I try to retrieve the register from the binary file:

Register auxRegister = Register();

while(!file.eof()) { //I kwnow this is not right. Which is the right way?

    file.read(reinterpret_cast<char*>(&auxRegister), sizeof(Register));
}

It occurs it does not work. Register does, in fact, have more attributes (they are int) and I retrieve them OK, but it's not the case with the string.

Am I doing something wrong? Should I take something into consideration when working with binary files and strings?

Thank you very much.

2 Answers 2

2

The std::string class contains a pointer to a buffer where the string is stored (along with other member variables). The string buffer itself is not a part of the class. So writing out the contents of an instance of the class is not going to work, since the string will never be part of what you dump into the file, if you do it that way. You need to get a pointer to the string and write that.

Register auxRegister = Register();   
auxRegister.setName("name");
auto length = auxRegister.size();

for(int i = 0; i < 10; i++) {
  file.write( auxRegister.c_str(), length );
  // You'll need to multiply length by sizeof(CharType) if you 
  // use a wstring instead of string
}

Later on, to read the string, you'll have to keep track of the number of bytes that were written to the file; or maybe fetch that information from the file itself, depending on the file format.

std::unique_ptr<char[]> buffer( new char[length + 1] );
file.read( buffer, length );

buffer[length] = '\0'; // NULL terminate the string
Register auxRegister = Register();

auxRegister.setName( buffer );
Sign up to request clarification or add additional context in comments.

Comments

2

You cannot write string this way, as it almost certainly contains pointers to some structs and other binary stuff that cannot be serialized at all. You need to write your own serializing function, and write the string length + bytes (for example) or use complete library, for example, protobuf, which can solve serializing problem for you.

edit: see praetorian's answer. much better than mine (even with lower score at time of this edit).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.