Is there a way to create multiple copies of a file in C++?
Hi I need to make multiple copies of a file using Dev-C++ can anyone help?
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
just write it multiple times using a different filename each time. Or write the first file then call os-specific command to make additional copies.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
just write it multiple times using a different filename each time. Or write the first file then call os-specific command to make additional copies.
I'm trying to make multiple copes of the same file, like: filename[1].ext, filename[2].ext, etc... and windows is my platform. The only commmand that I know of is the "CopyFile" command of windows.h and I don't think I can make multiple file copes with that, can you help me?
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
why do you think CopyFile won't work for you? Its one of the easiest win32 api functions you can use.
for(i = 0; i < NumberFiles; i++)
{
CopyFile(OriginalFilename, filename[i], FALSE);
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
why do you think CopyFile won't work for you? Its one of the easiest win32 api functions you can use.
for(i = 0; i < NumberFiles; i++)
{
CopyFile(OriginalFilename, filename[i], FALSE);
}
I figured thought that the "CopyFile" function could only take certain arguments. And I tried
your suggestion and I was proven right. The error message said:
-- invalid conversion from `const char' to `const CHAR*'--
This is my code thus far,
//Multiple File copy module
#include<iostream>
#include <windows.h>
using namespace std;
int main (void)
{
int n;//where "n" is the number of fies to be copied.
for (n=0; n<2; n++){
CopyFile("C:\\Dev-Cpp\\Programs\\MultipleFileCopyModule.exe","C:\\TestBox\\MultipleFileCopyModule.exe"[n],false);
//The file copies itself.
cout << "\t\t\t\tFile copied\n\n";
system ("pause");
}
return 0;
}
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
> -- invalid conversion from `const char' to `const CHAR*'--
The [n] subscript is doing that.
You need to generate the filename string, with n appended, before you pass it to CopyFile().
In C, I'd use say sprintf(), but in C++ I think a string stream would be more appropriate.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Lets say you want the file names to be
MultipleFileCopyModule1.exe
MultipleFileCopyModule2.exe
MultipleFileCopyModule3.exe
To accomplish that you have a couple options:
1:) call CopyFile three times
std::string filename = "C:\\Dev-Cpp\\Programs\\MultipleFileCopyModule";
CopyFile((std::string)(filename+".exe").c_str(), (std::string)(filename+"1.exe").c_str(),false);
CopyFile((std::string)(filename+".exe").c_str(), (std::string)(filename+"2.exe").c_str(),false);
CopyFile((std::string)(filename+".exe").c_str(), (std::string)(filename+"3.exe").c_str(),false);
2) Use a loop if the number of names is unknown at compile time, something like this:
#include <sstream>
#include <string>
...
...
std::string filename = "C:\\Dev-Cpp\\Programs\\MultipleFileCopyModule";
for(int i = 0; i < n; i++)
{
std::stringstream newname;
// create a new filename
newname << filename << i << ".exe";
// copy the file
CopyFile((std::string)(filename+".exe").c_str(), newname.str(), false);
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Aww man!, I totally thought that would work, but now the compiler says:
--- cannot convert `std::string' to `const CHAR*' for argument `1' to `BOOL CopyFileA(const CHAR*, const CHAR*, int)' ---
Have any suggestions?
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
post code. you probably need to pass the c_str() method from the std::string object, as I illustrated in my previous post.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
post code. you probably need to pass the c_str() method from the std::string object, as I illustrated in my previous post.
You were right I needed to used the ".c_str()". Thanks!!
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0