Jump to content
  • Advertisement
TiNo Martin

C++ Serialize a vector of strings

Recommended Posts

How can I write and read a `std::vector<std::string>` into binary file in C or C++? Withour using an external library.

Edited by TiNo Martin

Share this post


Link to post
Share on other sites
Advertisement

First you need to open a file

FILE * f = fopen("/mnt/sd_card/mybinstrvec.vtxt", ),"wb+");

Then at first the best would be to save vector size (how many strings you have)

int vsize = int ( myvector.size() );

fwrite(&vsize, sizeof ( int ), 1, f);

Then the best would be to convert std::string to * unsigned char

but you could just put a loop and read uchar from each element of string then fwrite it but before that you should write the length of the string so you'll know when to finish updating the string.

Then you fclose(f)

And with opening that

Read the size of vector to know how many string we will read

FILE * f = fopen("/mnt/sd_card/mybinstrvec.vtxt", ),"rb");

Then for (int i=0; i < str_count; i++)

{

Read actual string length

Either fread individually each char and update it to string or read a whole to pchar and create string from that

}

fclose(f);

Share this post


Link to post
Share on other sites

The above answer covers C idioms, but it might be worth mentioning that there are other ways of doing this that might be more idiomatic with respect to C++.

Also, just out of curiosity, is this a homework question by any chance? (Not saying it is - it just sounds like the kind of thing that might be a homework question.)

Share this post


Link to post
Share on other sites

@_WeirdCat_ Thanks for the answer!

1 hour ago, Zakwayda said:

The above answer covers C idioms, but it might be worth mentioning that there are other ways of doing this that might be more idiomatic with respect to C++.

Also, just out of curiosity, is this a homework question by any chance? (Not saying it is - it just sounds like the kind of thing that might be a homework question.)

What would be the version en C++? And is not for homework, is for a personal project.

Share this post


Link to post
Share on other sites
1 hour ago, TiNo Martin said:

What would be the version en C++?

Not trying to be rude, but a 2 second Google search turned up multiple working examples:

https://stackoverflow.com/questions/27523273/c-properly-writing-stdstring-to-binary-file

https://stackoverflow.com/questions/10873382/write-and-read-string-to-binary-file-c

If you're not sure how to output a vector of strings I think you might be putting the cart before the horse and should start learning the very basics of C/C++.  Also @_WeirdCat_'s code is not complete, there is no error checking and he casts vector::size() from an unsigned to a signed for no reason what-so-ever (it's actually just a std::size_t in most implementations).  Also, not sure why he wants you to write to "/mnt/sd_card/..." where a filename in the local directory would be more succinct.  The below code does error checking and correctly writes out a string to the file.  You will, as @_WeirdCat_ points out, want to record how many elements are in your vector so you can correctly read them back.  You might also want to write how many characters each string contains too.  I'll leave that and learning how to use fread to you, if you have issues feel free to post your code and ask for help explaining what you've tried to solve the problem yourself.  Good luck!
 

std::string some_text = "My text to write to the file.";

FILE* file = fopen("filename.bin", "wb");
if (file != NULL)
{
  fwrite(some_text.c_str(), sizeof(char), some_text.length(), file);
  fclose(file);
}

 

Edited by CrazyCdn
Fixed a few typos.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!