>> if (i < buff.size() + 1) o << ",";

That condition will always be true, so...

>> if (i % 21) o << "\n";

You probably want to be checking for a remainder of zero there, don't you think?