我想讀取由30e6個位置組成的大的二進制文件,每個文件都有195個雙打。 由於文件太大而無法全部讀入內存,因此我正在以10000個位置的塊讀取它。然後我做了一些計算,並閱讀下一個塊... ...fread有時會返回大於4GiB的文件的錯誤值
因爲我需要隨機訪問該文件,我寫了一個函數來從文件和商店讀取給定的塊(無符號整數塊)它在** chunk_data中。該函數返回讀取的位置總數。
unsigned int read_chunk(double **chunk_data, unsigned int chunk) {
FILE *in_glf_fh;
unsigned int total_bytes_read = 0;
// Define chunk start and end positions
unsigned int start_pos = chunk * 10000;
unsigned int end_pos = start_pos + 10000 - 1;
unsigned int chunk_size = end_pos - start_pos + 1;
// Open input file
in_glf_fh = fopen(in_glf, "rb");
if(in_glf_fh == NULL)
error("ERROR: cannot open file!");
// Search start position
if(fseek(in_glf_fh, start_pos * 195 * sizeof(double), SEEK_SET) != 0)
error("ERROR: cannot seek file!");
// Read data from file
for(unsigned int c = 0; c < chunk_size; c++) {
unsigned int bytes_read = fread ((void*) chunk_data[c], sizeof(double), 195, in_glf_fh);
if(bytes_read != 195 && !feof(in_glf_fh))
error("ERROR: cannot read file!");
total_bytes_read += bytes_read;
}
fclose(in_glf_fh);
return(total_bytes_read/195);
}
的問題,閱讀一些大塊之後,fread()
開始給錯誤的價值觀! 此外,根據不同的塊大小,其中fread()
開始表現的立場不同,很奇怪:
chunk of 1 pos, wrong at chunk 22025475
chunk of 10000 pos, wrong at chunk 2203
chunk of 100000 pos, wrong at chunk 221
任何人有什麼可能會在任何想法?
什麼是'的sizeof(int)的'上你的平臺? –
問題的標題應該是有意義的。它將幫助用戶總結您的問題。 –
你已經爲塊分配了內存? –