簡體   English   中英

Windows函數ReadFile不斷返回FALSE,不知道為什么

[英]Windows function ReadFile keeps returning FALSE and don't know why

讀取文件文本是一個非常簡單的C代碼,我已經做過幾次,但是不知道為什么現在ReadFile函數不斷返回FALSE意味着失敗。

顯然,文本文件input.txt存在並且CreateFile函數成功(或至少不返回INVALID_HANDLE_VALUE

#include <stdio.h>
#include <windows.h>

int main(int argc, char *argv[])
{
    char ReadBuffer[256] = {0};
    HANDLE hFile1;
    int n = 0;
    DWORD bytesread = 5;

    printf("Press enter to read the file 'input.txt' ... ");
    getch();

    if ( (hFile1 = CreateFile(TEXT("input.txt"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL) == INVALID_HANDLE_VALUE))
    {
        printf("INVALID_HANDLE_VALUE");
        return -1;
    }

    if ( ReadFile(hFile1, ReadBuffer, bytesread, &bytesread, NULL) == FALSE )
    {
        printf("ReadFile ERROR");
        CloseHandle(hFile1);
        return -2;
    }

    printf("\n\nRead bytes: %d \n\n", bytesread);


    CloseHandle(hFile1);
    return 0;
}

該錯誤是括號的簡單錯位。

if ( (hFile1 = CreateFile(TEXT("input.txt"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM