This is the wrong thing:
this is the correct thing:Code:out.open("page"+pageNo+".html");
They are not that different. So there is nothing more simple than the above as the correct answer.Code:out.open((std::string("page")+pageNo+".html").c_str());
About efficiency I don't think there is a huge difference.
I would expect them to have the same speed more or less. Of course you give the total size so it allocates only once. You could call string::reserve for that to be fair.
But in any case you allocate space, initialize it, and write again on that memory.
The above "correct" method allocates space, initializes it, allocates space, initializes, allocates space initializes it.
Since the string is not really big there won't be probably any need to copy data for the std::string. So it is 3 allocations vs 1 allocation and rewriting a memory space (page).
Now, do this with reserve (which is fair). Now you allocate space, initialize on part, initialize other part, initialize other part.
So it is 3 allocations+initialization and 1 rewrite vs 3 allocations+initialization. So the std::string method might actually be faster.
Of course the std::string will have some function calls overhead. But those are not that big of a time waste. And they are totally worth it.
But the point is that you might NOT know the size. Especially considering that pageNo WILL change. It might go over 9. So you would need one more space.