CopyFile() Confusion

Alright so I'm just trying to figure out how to use the CopyFile() function in C++. So far I can't figure out any of it. What I'm running is:

1
2
3
4
5
6
7
8
9
10
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
	CopyFile("C:\\Program Files\\Warcraft III\\War3Inst.log", "C:\\Program Files\\Argh.log", 1);
	return 0;
}


It seems to run fine, but nothing ever gets copied.

The debug info is:

'copyfiletest.exe': Loaded 'C:\Users\Nick\Desktop\Programming\Projects\copyfiletest\Debug\copyfiletest.exe', Symbols loaded.
'copyfiletest.exe': Loaded 'C:\Windows\System32\ntdll.dll', Symbols loaded (source information stripped).
'copyfiletest.exe': Loaded 'C:\Windows\System32\kernel32.dll', Symbols loaded (source information stripped).
'copyfiletest.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Symbols loaded (source information stripped).
'copyfiletest.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
The program '[2068] copyfiletest.exe: Native' has exited with code 0 (0x0).
Last edited on
Try:
1
2
3
4
5
6
7
8
9
10
11
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
	CopyFile("C:\\Program Files\\Warcraft III\\War3Inst.log", "C:\\Program Files\\Argh.log", 1);
	DWORD Error = GetLastError();
	return 0;
}

Tell us which value does Error have then.
It can also be that there is already a file in C:\Program Files named Argh.log and it fails. If you don't want it to fail change the ending '1' to '0' (you should).
Also C:\program files directory may be one of those 'special' locations that
need admininstrator rights to be able to create files directly.
Nothing seems to be different with your build. I don't think
DWORD Error = GetLastError();
did anything?

If I add in
cout<<Error<<endl;
though, I get '5', and I have no idea what it means? I'm still a beginner...
Look here:
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms681382%28v=vs.85%29.aspx

Look down the page.
Error code 5 is ERROR_ACCESS_DENIED

Check my earlier post (of about 30 minutes ago)
Last edited on
Yes, what I wanted was that '5' number and guestgulkan is right - You need admin privileges for Vista and 7 to access those folders.
That means you have to run Visual Studio as administrator, as you seem to debug your program.
YES! I just ran it as administrator and it is working fine! Thank you so much everyone!
Topic archived. No new replies allowed.