Hi I need to make multiple copies of a file using Dev-C++ can anyone help?

Recommended Answers

All 9 Replies

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.

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?

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);
}

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;
}

> -- 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.

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);
}

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?

post code. you probably need to pass the c_str() method from the std::string object, as I illustrated in my previous post.

commented: perfect! +1

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!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.