From Cinder Asset to ifstream

#1

Hey Embers,

The title more or less says it all. I’ve got a vector<vector<float>> that contains some data I need in order to simulate some behavior. I looked around ‘teh webz’ a bit and found this helpful stackoverflow answer that pointed me towards boost serialization.

The only problem is that I have no idea how (or even if it’s possible) to go from a DataSource to an ifstream. I tried putting together the following code instead

auto csvFileRef = ci::app::loadResource( RES_FFT_DATA );
auto csvDataString = ci::loadString( csvFileRef );
std::istringstream test( csvDataString );

boost::archive::binary_iarchive ia( test );
//boost::archive::binary_iarchive ia( in );
ia >> features;
//in.close();

But the csvDataString ends up only containing a single char, and then probably fails because the data isn’t actually originally a string.

Any ideas of how to pass the binary data from an asset into a format I can pipe into an ifstream would be much appreciated!

Cheers,

Gazoo

0 Likes

#2

For those interested, I had some minor issues actually getting saving/loading a vector<vector<float>> to and from a file on disk, but I eventually got that sorted. Here’s the complete code which should work:

void SaveFeaturesFile( std::vector<std::vector<float>> const& features, std::string filename )
{
	std::ofstream out( filename.c_str(), std::ios::binary );
	boost::archive::binary_oarchive oa( out );
	oa << features;
	out.close();
}

void LoadFeaturesFileFromFile( std::vector<std::vector<float>> &features, std::string filename )
{
	std::ifstream in( filename.c_str(), std::ios::binary );
	boost::archive::binary_iarchive ia( in );
	ia >> features;
	in.close();
}

Note, this is platform dependent due to the data being stored in binary!

I also came across this previous topic which elaborates a bit on how to create std::stringstream from a cinder buffer, but I’ve yet to grasp how to work things out for binary data.

Cheers,
Gazoo

0 Likes