You need to either pass a correctly formed string as argument to the constructor. For example,
std::ofstream myfile("key" + std::to_string(i) + ".txt");
or
#include <sstream> // for std::ostringstream
std::ostringstream strm;
strm << "key" << i << ".txt";
std::ofstream myfile(strm.str());
The above assumes you have a compiler supporting C++11. If you don't, this a slight variation on the second example would work:
std::ofstream myfile(strm.str().c_str());
^^^^^^^