Like adeyblue said, you have the newline stuck to the end of the buffer read from the console, which causes your error. This works just fine:
Code:
#include <windows.h>
#include <stdio.h>
int main()
{
char fileName[MAX_PATH] = { 0 };
printf("Enter filename to create: ");
fgets(fileName, ARRAYSIZE(fileName), stdin);
// Without the following line, the newline is still attached.
fileName[strlen(fileName) - 1] = '\0';
wchar_t wideFileName[MAX_PATH];
mbstowcs(wideFileName, fileName, ARRAYSIZE(wideFileName));
HANDLE h = CreateFile(wideFileName,
GENERIC_WRITE,// Open for writing
0, // Do not share
NULL, // Default security
OPEN_ALWAYS, // Open file in all cases
FILE_ATTRIBUTE_NORMAL |// Normal file
FILE_FLAG_NO_BUFFERING | // No System buffer
FILE_FLAG_WRITE_THROUGH, // Write straight to the disk
NULL); // That's all folks!
if (INVALID_HANDLE_VALUE == h)
{
printf("Error: %lu\n", GetLastError());
}
return 0;
}