CopyFile Function does not accept any type

I'm trying to create a program that will copy a folder automatically every 240 minute (or every 4 hour). I found the CopyFile function would suit my needs.

The problem I am experiencing is that no matter what type of string or character array I put into the first two arguments wont get accepted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "stdafx.h"
#include <iostream>
#include <Windows.h>

using namespace std;

int timer();

int main()
{
	while (1)
	{
		const char * f = "test.txt";
		const char * newF = "test1.txt";

		int timerV = timer();

		if (timerV == 240) //240 minutes
			if (CopyFile(f, newF, false))
			{

			}
                        else
                        	cout << "Failed to execute copy.\n";
		else
			cout << "Failed to execute copy.\n";
	}

	cin.get();

    return 0;
}

int timer()
{
	int time = 0;

	while (time < 240) //240 minutes
	{
		Sleep(60000); //1 minute
		time++; //add 1
	}

	return time;
}


I've tried using a regular character array, a string object, const char, like I used in this example, and also typing the string directly into the function, not using a variable, like so:
 
CopyFile("test.txt", "test1.txt", false)


When trying to compile any of these I get the error "argument of type (type) is incompatible with parameter of type "LPCWSTR""

Nothing seems to work..
Last edited on
In what way are they "not accepted"? Does the code not compile?
Sorry, forgot to add the error, I have edited the question now, its at the bottom
This is a problem involving VS project settings and unicode and so on.

Try sticking _T in front of your strings:

CopyFile(_T("test.txt"), _T("test1.txt"), false)
This works, thank you. However, I need variables to get this working the way I want it to, I'm just lost at which type I should use
Perhaps std::string objects: CopyFile(_T(some_string_object.c_str()), _T(some_other_string_object.c_str()), false)

Another alternative - and this applies to many windows functions, is to add the suffix A for ordinary characters and W for wide-characters. Hence CopyFile becomes CopyFileA.

1
2
3
4
    if (!CopyFileA(f, newF, false))
    {
        cout << "Failed to execute copy.\n";    
    }

As an aside, if you're using a recent MS compiler, it will have support for the experimental C++ filesystem library and you can just use that, without having to worry about unicode/wide/ordinary/etc.
Chaost wrote:
When using this code, the "result" variable outputs "0", that's it. I don't get any errors or warnings in my compiler either.

That's good, isn't it?

From the reference page,
MSDN wrote:
Return value

Type: HRESULT

If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Looking up the meaning of S_OK, it has the value 0.

1
2
3
4
5
6
7
    TCHAR documents[MAX_PATH];
    HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, documents);

    if (result == S_OK)
        cout << "Path: " << documents << '\n';    
    else
        cout << "Error: returrn value = " << result << '\n'; 

References:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137(v=vs.85).aspx
Topic archived. No new replies allowed.