There's unfortunately a great deal going wrong here. Your program
invokes undefined behavior, because the buffers you pass to strcat do
not have sufficient size to hold the concatenated strings. If you're
going to use the old C libraries, you should at least read up on them
to avoid such mistakes:
http://www.cplusplus.com/ref/cstring/strcat.html
That said, it's a bad idea to be using all this legacy, C-style stuff
in any case. Unless you are under some absurd constraint to do so, you
should instead use the STL's string, and associated practices. Here is
an updated, working version of your code:
#include <iostream> // for std::cout
#include <string> // for std::string
#include <sstream> // for std::stringstream
std::string createFileName(int fileIndex) {
std::stringstream stream;
stream << "file" << fileIndex << ".txt";
stream.flush();
return stream.str();
}
int main(void) {
for(int i=1; i<=5; i++) {
std::cout << createFileName(i) << "\n";
}
return 0;
}
Luke