-
File Copier
I'm pretty much a noob at c++, but I'm trying.
Ok basically this progam is supposed to make a file 5 times, using a do while. However every time the loop restarts it writes the file like its supposed to and uses the same file name like expected but this is just the problem, since the file name is the same it only writes one file instead of 5, how can the file name to be used, be changed every time the loop iterates? :confused:
Code:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char str[10];
int x=0;
do
{
x++;
ofstream a_file ( "file.txt" );
a_file<<"test";
a_file.close();
}
while(x<5);
}
-
Instead of hardcoding "file.txt" into the ofstream line, use a char string. Then you can simply change one character of the string every time through the loop.
-
I understand what you are saying but I am not sure how you would change one character or any character in the string automatically. What I tried was I left the x as an int, but in the ofstream line I used (char)x to convert it into a char (and the counter would change the name each time), however the compiler says the file name cant be a char but a const char. Is there a way of changing a const char in each iteration?
-
Hello,
Here is my solution to your problem.
Code:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char str[] = "file .txt";
char str2[] = "01234";
int x=0;
do
{
str[4] = str2[x];
ofstream a_file ( str );
a_file<<"test";
a_file.close();
x++;
}
while(x < 5);
}
EDIT
swoopy's solution is better because it easily allows for more values of x then just x < 10
-
One idea is sprintf().
Code:
#include <cstdio>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char filename[80];
int x=0;
do
{
x++;
sprintf(filename,"file%d.txt",x);
ofstream a_file ( filename );
a_file<<"test";
a_file.close();
}
while(x<5);
}
-
:D Hey thanks swoopy, that was the perfect solution, and I'm going to need to spend some time looking at it to figure out how it works,
just one more question, instead of just creating a file and copying it 5 times, how could you take an already created file and copy it 5 times?
-
Here's one way.
Code:
#include <cstdio>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char src_filename[80] = "filename.xxx";
char filename[80];
int x=0;
ifstream in(src_filename,ios::binary);
if (!in.is_open())
{
cout << "File not found: " << src_filename << endl;
return 1;
}
do
{
x++;
sprintf(filename,"file%d.txt",x);
ofstream a_file ( filename, ios::binary );
a_file << in.rdbuf();
a_file.close();
in.seekg(0);
}
while(x<5);
cout << "File " << src_filename << " copied." << endl;
}
You can also use CopyFile() if you are using windows. To use it, include <windows.h>.
-
A third option is the system() command. Something like:
Code:
system("copy a.txt b.txt");
or whatever the file copy command is on your computer.
-
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int i = 0;
do
{
char file [200];
itoa(i,file,10);
strcat(file,".txt");
ofstream a_file(file, ios::app);
i++;
}
while (i < 5);
}
Something like this will do you fine just modify the code for your needs.