Brans Ds
Brans Ds

Reputation: 4117

Strange CreateFile behaviour

Strange CreateFile behaviour. I am tiring to open file

HANDLE hFile = CreateFile(L"E:\\temp\\1.txt",  
    GENERIC_READ,          
    FILE_SHARE_WRITE,     
    NULL,                   
    OPEN_ALWAYS,             
    FILE_ATTRIBUTE_NORMAL, 
    NULL); 

if (hFile == INVALID_HANDLE_VALUE)
{
    printf("Could not CreateFile\n");
    return;
}

File is opened no error (don't know is it target file).. for some reason in the directory where is my application running empty file "E"(wiout extention) created during CreateFile call. What is wrong here?

Upvotes: 0

Views: 214

Answers (1)

Ferruccio
Ferruccio

Reputation: 100638

Get rid of the L prefix of your filename string. You are calling the narrow version of CreateFile (CreateFileA) and passing in a wide string.

Alternatively, you can set VS to build your app using Unicode.

In either case, you should use the _T() macro to set the appropriate string type. i.e.

HANDLE hFile = CreateFile(_T("E:\\temp\\1.txt"),

Upvotes: 9

Related Questions