我正在尝试读取一个由 30e6 个位置组成的大二进制文件,每个位置有 195 个双精度数。由于文件太大而无法全部读入内存,因此我按 10000 个位置分块读取它。然后我用它进行一些计算并读取下一个块......
由于我需要随机访问文件,因此我编写了一个函数来从文件中读取给定块(无符号整数块)并将其存储在 **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, …Run Code Online (Sandbox Code Playgroud)