Can anybody please tell me why iam getting an unexpected run-time error for the code given below. It works for two times iteration but not for more than that.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void print(string hmm)
{
ofstream ko(hmm.c_str(),ios::trunc);
ko<<"chacho";
ko.close();
}
int main(){
for(int i=0;i<5;i++)
{
char *chat=new char;
sprintf(chat,"%d%s",i,"_num.txt");
string rat=chat;
print(rat);
}
system("pause");
return 0;
}
constin the function you need would be preferable, but you can use a static const in a class or a const in a namespace. Even a const in the global namespace is much better than a define, as define goes before the compiler starts and ignores scope. If there's any sort of conflict, you're going to waste some time trying to figure out what the weird error you're getting is. Here's a link to a previously answered question: stackoverflow.com/questions/4715831/…