Dound with CopyFile()

well I coded this:
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include <string>
#include <stdio.h>
#include <windows.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;;

string getExePath(){
char result[ MAX_PATH ];
return std::string( result, GetModuleFileName( NULL, result, MAX_PATH ) );
}

main(){
cout <<"My directory is" << getExePath() << "\n";
CopyFile("C:\\PerfLogs\\teste.exe","C:\\User\\test.exe",FALSE);
}

Well, it is just a little bit of the code. But I want to put the getExePath() string inside the CopyFile. So, instead I use C:\\PerfLogs\\teste.exe, I will put the getExePath() path.

Is there any way???

Please, try to write the code of solutions, because my english is terrible. TKS
First, you'll need some functions to manipulate file paths. Like these:
http://www.cplusplus.com/forum/general/34348/#msg185786

Then you can swap things around.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
  string exe_path = getExePath();
  cout << "My exe file is: "        << ExtractFilename( exe_path, '\\' ) << "\n";
  cout << "My exe's directory is: " << ExtractDirectory( exe_path, '\\' ) << "\n";
  CopyFile(
    (ExtractDirectory( exe_path, '\\' ) + "teste.exe").c_str(),  // destination -- same folder as my exe
    "C:\\User\\test.exe",  // source -- hardcoded filename
    FALSE
    );
}

Try to avoid using pathnames that are hardcoded (like "C:\\User\\test.exe") if at all possible.


If you have Boost installed, you can do all this with Boost::Filesystem.

Hope this helps.
Topic archived. No new replies allowed.