簡體   English   中英

fread_s讀取錯誤的字節數

[英]fread_s reading the wrong amount of bytes

我正在讀取帶有fread_s的收集文件(一個中包含20個左右的小文件),並且內容正在struct中寫入。 就像99%的時間一樣,它正確地讀取數據,但是一次,在總是相同的位置,它似乎忽略了元素大小參數的字節大小,只是讀取500個左右的字節,直到中止並報告feof錯誤為止。 關鍵是,它甚至沒有將int的最后三個字節寫入結構。

當我刪除支票並讓它繼續閱讀時,它將再次恢復正常,就像什么也沒有發生。

我觀察到文件指針中的_Placeholder變量被更改為另一個值,然后再次返回,但是我猜想它只是eof錯誤被打包在那里。

#pragma pack(push, 1)
struct fileHeader {
    __int32 typeID;
    bool isGFX;
    char filename[8];
    __int32 offset;
};
#pragma pack(pop)

#define HEADERSIZE 68
#define FILEHEADERSIZE 17

....
FILE *file;
fopen_s(&file, filename.c_str(), "r");

for (int i = 0; i < header.files - 1; i++) {
    fseek(file, HEADERSIZE + i * FILEHEADERSIZE, 0);

    fileHeader headerFile;
    memset(&headerFile, 0, FILEHEADERSIZE);

    int oldPointer = ftell(file); //118
    int d = fread_s(&headerFile, FILEHEADERSIZE, FILEHEADERSIZE, 1, file); //returns 0
    int newPointer = ftell(file); //630

    int e = errno; //0
    int ea = ferror(file); //0
    int ef = feof(file); //1

    //getting used here in a function
}
headerFile = {typeID=17 isGFX=true filename=0x00fefd05 "CURSORR" offset = 164} - offset should be 6820

就像喬納森·萊夫勒(Jonathan Leffler)在評論中說的那樣,錯誤是我沒有以二進制模式閱讀。 一個簡單的更改fopen_s(&file, filename.c_str(), "r"); fopen_s(&file, filename.c_str(), "rb"); 解決了問題。

暫無
暫無

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

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