# 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 on other sites

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++)

{

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

}

fclose(f);

##### 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 on other sites

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 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:

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.

## Create an account

Register a new account

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 18
• 15
• 9
• 9