I'm having trouble using the CreateFile() and WriteFile() functions. Here's the code I've written:

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
    char cData[6];
    DWORD dwBytesWritten;
    cout << "Please enter a number: ";
    cin >> cData[1];
    for(int i = 2; i < 5; i++)
    {
        cout << "\nPlease enter another number: ";
        cin >> cData[i];
    }
    for(int i = 0; i < 5; i++)
    {
        cout << cData[i] << endl;
    }

    HANDLE hFile = CreateFile(TEXT("FileBGP.txt"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    BOOL WriteFile(hFile, &cData, 5, &dwBytesWritten, NULL);
}

The program should allow the user to enter 5 characters/numbers, then write them to a file. However, I get a compile error on BOOL WriteFile() saying this:

error: initializer expression list treated as compound expression

What am I doing wrong here?

Is there a particular reason you put the BOOL in there? WriteFile() is a function. On Line 23, you're not calling the function, you're attempting to instantiate an object of the BOOL type called WriteFile.

I suggest you remove the BOOL.

Oh, of course. I put that in there because my programming book had it in there. Thanks for showing me that simple mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.