I wanted to copy a file multiple times using different names.
The program is this:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <sstream>
#include<cstring>
using namespace std;
main()
{
string text;
int i;
char ch;
ostringstream oss;
FILE *fp1,*fp2;
if((fp1=fopen("One Dollar.jpg", "rb"))==NULL)
{
cout<<"Error";
exit(-1);
}
for(i=1; i<=5; i++)
{
oss << "C:\\5241 Dollar\\One Dollar " << i << ".jpg";
text = oss.str();
if((fp2=fopen(text.c_str(), "wb"))==NULL)
{
cout<<"Error "<<i;
exit(-1);
}
while(!feof(fp1))
{
fread(&ch, 1, 1, fp1);
fwrite(&ch, 1, 1, fp2);
}
fclose(fp2);
/* for(int j=0;j<30000;j++)
for(int k=0;k<30000;k++)
if(k==3000)
cout<<k; */
}
fclose(fp1);
}
In this there are two file streams one of which is source and the other is destination.. I loaded the actual file in binary read mode and the destination as binary write mode. I used a for loop to do the work. But as soon as the loop iterates 2nd time, the file opening of fp2 fails. I'm getting the output: Error 2.
How can I make the code work?