Question

I'm attempting to do something resembling the following block of code:

tf::transform t;
initializeTransform(t);
std::ofstream f;
f.open("somefile");
f << t << std::endl;
f.close();

Assuming that I've properly set up that f and t when I'm trying to write t to f, how would I do so? I tried a number of variants of this, and all of them result in a huge wall of text to the effect that ofstream doesn't know how to handle a tf::transform object, which isn't too surprising.

Is there some way to make ofstream take arbitrary objects? Is there some format that I could readily convert it to that's more conducive to streaming? Ideally, if I convert it, I'd like to have a way to reversibly convert it to some matrix that I can pipe straight into and out of a file.

Was it helpful?

Solution

Implement the operator
I'm not sure of the contents of the transform struct in this case, but assuming it is:

struct transform { float mat[16]; }

Then the implementation can be something like:

std::ostream& operator<< (std::ostream& os, const tf::transform& t)
{
  os << t.mat[0];
  for(int i=1;i<16;++i) os << ',' << t.mat[i];
  return os;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top