简体   繁体   中英

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM