Could someone show me how to properley use the ReadFile(); function? I can't seem to figure out how to use it. Thanks.
Printable View
Could someone show me how to properley use the ReadFile(); function? I can't seem to figure out how to use it. Thanks.
Code:BOOL ReadFile(
HANDLE hFile, // handle of file to read
LPVOID lpBuffer, // pointer to buffer that receives data
DWORD nNumberOfBytesToRead, // number of bytes to read
LPDWORD lpNumberOfBytesRead, // pointer to number of bytes read
LPOVERLAPPED lpOverlapped // pointer to structure for data
);
I think I understand what everything does now, but I still can't seem to make it work.
Could you give me an example to work with? Thanks.
To read 10 bytes from a file:
If a function dosen't work check the return code and call GetLastError to get an error code which will describe the problem in more detail.Code:char Buffer[10];
DWORD dwBytes;
HANDLE hFile;
hFile=CreateFile...
ReadFile(hFile, Buffer, 10, &dwBytes, NULL);
hFile=CreateFile...
I don't get it. Why would you create a file just to read a file?
Because Create file is used to open files as well as create them.
I tried this, but if something is in my file it erases the contence then reads it. How can I prevent that?
Thanks.Code:char Buffer[10];
DWORD dwBytes;
HANDLE hFile;
hFile=CreateFile("draft.txt", GENERIC_READ, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
ReadFile(hFile, Buffer, 10, &dwBytes, NULL);
MessageBox(NULL, Buffer, "Found", MB_OK);
change CREATE_ALWAYS to OPEN_EXISTING
Now it works. Thank for your help Quantum1024! :)