Creating multiple files using array in C++

I am trying to create n number of files (n being an integer), by passing the name through a character array (and obviously changing its value at each iteration). But the problem is that the program compiles and executes but not a single file is created.

Here is my code snippet.

void file_phipsi(int m)
{
int a=0,n=0;
char *str1;
while (m!=0)
{
n=str[a].length();
str1=new char[n+6];
strcpy(str1,str[a].c_str());
strcat(str1,".txt");
cout<<str1;
fstream f(str1,ios::out);
a=a+3;
m--;
f.close();
}
}
Files are created for me. Do you look in the program working folder?

Unrelated: why are you increasing a by 3? What is the content of str array? Do you use C++11 because your program way more complex than it should and additionally you ahve a memory leak here.

EDIT:
Try to do f.flush() before closing
Last edited on
How do you recognize a memory leak, like in here?
How do you recognize a memory leak
Having a new[] without accompaning delete[] usually a sure sign of a leak. As you allocating memory in loop, it gets worse.

By the way did you solve your original problem?
Ya I did. Thanks MiiNiPaa.
Topic archived. No new replies allowed.