Well, what do you want to have in the file?
If you want a "human readable" text file (e.g. CSV), then there will be no
other way than iterating the vector, convert each of the three fields of each
element into text, and write that text into the output file.
...either that, or use some sort of "serialization" library.
Of course, you can always just dump your
elements into the file as "binary" data, if you want to:
file.open("test.bin", std::ios::out | std::ios::trunc | std::ios::binary);
for (vector<Test>::const_iterator iter = vec.cbegin(); iter != vec.cend(); iter++)
But then you should also change your struct like this, to ensure a fixed
size in a platform-independent
#define MAX_STRLEN 256U
Also note that you may need to deal with byte order
(LE vs. BE) when exchanging data between platforms!
(Note: We use a fixed-size
-array to store the string here, to ensure that each record has a fixed
size. If you want to store your strings with "true" variable
length, then some more work will be required; it could be achieved with a "flexible array member", but then writing/reading the struct will not
be simple anymore!)