How to write and read a vector of class type that includes data type of int, std::string, and juce::File?

I have a vector of class type:

class Random
	Random(int _x, std::string _y, juce::File _z)
		x = _x;
		y = _y;
		z = _z;
	int x;
	std::string y;
	juce::File z;
std::vector<Random> randoms;

And I can fill the vector with something like this:

void populate(int x, std::string y, juce::File z)
    randoms.push_back({ x, y, z });

But how can I save and reload the contents of the vector randoms that I’m using for this juce app?

I found that the juce library includes FileOutputStream and FileInputStream, which might be helpful for this issue. However, I couldn’t figure out how to implement it in my code, and I couldn’t find any examples.

1 Like

You will need to serialize the contents into some savable format. A good example is ValueTree which can serialize easily into an XML.

So, writing functions of fromValueTree() and toValueTree() of your custom data type would be a good start.

You can also create a juce::var, and append your data using juce::var::append, then call juce::var::writeToStream.
You can probably also just write the vector contents directly to the stream, but keep in mind that this will not be portable to other platforms due to the endianess issues:

stream.write (, vector.size() * sizeof (float));