discodowney
discodowney

Reputation: 1507

Can't get ReadFile to read from the file i just wrote to

Im writing to a file using WriteFile. That works fine. Its just a simple string:

"Test string, testing windows functions".

Im trying to read from the file now and compare with the string i write just to ensure its working properly. I have:

DWORD dwBytesRead;
char buff[128];
ReadFile(hFile, buff, 128, &dwBytesRead, NULL)

But its returning false for me. hFile is the handle I use when writing to the file. Can have any ideas about what might be going on?

EDIT (updated from comment):

I'm getting E_ACCESSDENIED from GetLastError(). Here is how i got hFile:

hFile = CreateFile (TEXT(movedFileName.c_str()),
                    GENERIC_WRITE,
                    0,
                    NULL,
                    OPEN_EXISTING,
                    FILE_ATTRIBUTE_NORMAL,
                    NULL);

Upvotes: 0

Views: 282

Answers (1)

hmjd
hmjd

Reputation: 122011

hFile has been opened for GENERIC_WRITE only. It needs to be opened with GENERIC_READ if you want to read from it as well as write to it:

hFile = CreateFile (TEXT(movedFileName.c_str()),
                    GENERIC_WRITE | GENERIC_READ,
                    0,
                    NULL,
                    OPEN_EXISTING,
                    FILE_ATTRIBUTE_NORMAL,
                    NULL);

Upvotes: 1

Related Questions