Why is fread reaching the EOF early?

前端 未结 3 1321
春和景丽
春和景丽 2020-12-06 03:57

I am writing a C library that reads a file into memory. It skips the first 54 bytes of the file (header) and then reads the remainder as data. I use fseek to determine the l

相关标签:
3条回答
  • 2020-12-06 04:34

    A solution, based on the previous answers:

        size_t bytesRead = 0;
        BYTE* localBuffer = new BYTE[bytesTotal];
        fseek(stream,headerLen,SEEK_SET);
            while(!feof(stream) && !ferror(stream)) {
            size_t result = fread(localBuffer+bytesRead,sizeof(BYTE),bytesTotal-
            bytesRead,stream);
        bytesRead+=result;
    }
    
    0 讨论(0)
  • 2020-12-06 04:40

    perhaps it's a binary mode issue. Try opening the file with "r+b" as the mode.

    EDIT: as noted in a comment "rb" is likely a better match to your original intent since "r+b" will open it for read/write and "rb" is read-only.

    0 讨论(0)
  • 2020-12-06 04:52

    Also worth noting that simply including binmode.obj into your link command will do this for you for all file opens.

    0 讨论(0)
提交回复
热议问题