How to read a file using readfile on Winapi

北城以北 提交于 2019-12-25 19:47:11

问题


I'm learning how to use in Winapi And I'm trying to read a file from My Computer But for some reason it doesn't work ...

HANDLE hFile;
//PVOID First_Bytes[2048];
char First_Bytes[2048];
DWORD dbr = 0;
hFile = CreateFile(L"d:\\My-File",GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL , NULL);   
if (hFile == INVALID_HANDLE_VALUE) {
    printf("Error %x", GetLastError());
    return 1;
}
if (ReadFile(hFile, &First_Bytes, 512, &dbr, NULL) == 0) {
    printf("ReadFile error: %x", GetLastError());
    return 1;
}
printf("%s", First_Bytes);
CloseHandle(hFile);

The console doesn't print anything.

What am I doing wrong?

  • I edited the code and add that errors checks. But still consul does not print anything

回答1:


The logical conclusion is that the first byte in your file is a zero. You treat the buffer as a null-terminated string, and so nothing is printed.

Do note that there is no guarantee that your buffer is null terminated so you potentially have undefined behaviour.



来源:https://stackoverflow.com/questions/33586466/how-to-read-a-file-using-readfile-on-winapi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!