Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualbelfegor

Posted 30 May 2013 - 04:37 AM

It doesn't matter what kind of and in what order do you put things, what matters is that you read those in order that you put them and you need to know their types and size.

Sometimes you must first write the count of objects for array type of things like vectors and strings, before you write those, so you can know how much to read later on:

 

write

 

std::vector<int> vInt;
...
std::size_t len = vInt.size();
os.write( (const char*)&len, sizeof(std::size_t) );
os.write( (const char*)&vInt[0], vInt.size() * sizeof(int) );

 

read

 

std::size_t len;
is.read( (char*)&len, sizeof(std::size_t) ); // first thing in file written is length so we read that first, sizeof(std::size_t) bytes
std::vector<int> vInt(len); // create buffer with enough space to read into it
is.read( (char*)&vInt[0], len * sizeof(int) ); // second thing in file is buffer to read, len * sizeof(int) bytes

#1belfegor

Posted 30 May 2013 - 04:33 AM

It doesn't matter what kind of and in what order do you put things, what matters is that you read those in order that you put them and you need to know their types and size.

Sometimes you must first write the count of objects for array type of things like vectors and strings, before you write those, so you can know how much to read later on:

 

write

std::vector<int> vInt;
...
std::size_t len = vInt.size();
os.write( (const char*)&len, sizeof(std::size_t) );
os.write( (const char*)&vInt[0], vInt.size() * sizeof(int) );

 

read

std::size_t len;
is.read( (char*)&len, sizeof(std::size_t) ); // first thing in file written is length so we read that first, sizeof(std::size_t) bytes
std::vector<int> vInt(len); // create and resize buffer size so it have enough space to read into it
is.read( (char*)&vInt[0], len * sizeof(int) ); // second thing in file is buffer to read, len * sizeof(int) bytes

PARTNERS