Search code examples
c++file-mapping

C++ too many initializer values on GENERIC_READ when creating a file mapping object


I'm trying to create a file mapping object but I'm experiencing a few compiler errors. (I'm using MinGW GCC-8.2.0-3)

I'm getting the following error from VS-code: too many initializer values on the GENERIC_READ line.

HANDLE CreateFile(
    L"filename.txt",
    GENERIC_READ,
    FILE_SHARE_READ,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL
);

When compiling the project I'm getting the following errors:

main.cpp: In function 'int main()':
main.cpp:18:5: error: expression list treated as compound expression in initializer [-fpermissive]
     );
     ^
main.cpp:18:5: error: invalid conversion from 'int' to 'HANDLE' {aka 'void*'} [-fpermissive]

I've tried replacing GENERIC_READ with GENERIC_READ | GENERIC_WRITE but the error with too many initializer value persists but only under the GENERIC_READ. Another thing I've tried with no avail was replacing CreateFile with CreateFileA. I'm including Windows.h.

I'm really confused on why this is happening, because under the WIN32 API Documentation it states that for the dwDesiredAccess it is commonly used as GENERIC_READ or GENERIC_WRITE or GENERIC_READ | GENERIC_WRITE

Thanks in advance!


Solution

  • Thanks to @Jarod42 for the answer.

    I had to assign the file handle to a variable:

    HANDLE fileHandle = CreateFile(
        _T("combatlog.txt"),
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );