if I did want to strcat() |
I really don't recommend it. But strcat works with c-style strings (char arrays).
strings (std::string) and stringstreams are completely different. And if you're using them, you don't need strcat at all.
Even in this case, if you're using char arrays, strcat wouldn't be a good idea. You'd be better off with sprintf. That way you don't have to mix the stringstream in there.
C-style approach (no strings, stringstreams):
1 2 3 4 5 6 7
|
char filename[30]; // note an array, not a pointer
for(...)
{
sprintf(filename,"datos/file_%d.txt",i);
mySaveFile.open(filename);
}
|
The problem with this is that there's a potential for overflow. Here, 'filename' can hold a string that is up to 29 characters long. Any longer and it will still compile OK, but you'll have memory corruption (
disasterous!!!)
Overflowing a string or stringstream like this is pretty much impossible, so they're much safer and simpler to use, which is why I recommend them.
Now, if you want to mix stringstreams and char arrays for whatever reason (even worse idea), and you want to use strcat... then you could do this:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
char filename[30]; // note an array, not a pointer
for(...)
{
ostringstream number;
number << i;
strcpy(filename,"datos/file_");
strcat(filename,i.str().c_str());
strcat(filename,".txt");
mySaveFile.open(filename);
}
|
But again this is silly and computationally more expensive than the above alternatives. You also still have the risk of overflowing 'filename' if you're not careful.