我正在尝试以块大小BLOCK_SIZE
(当前等于1000 unsigned chars
)将文件读入缓冲区。我的代码最初找到它必须读取的块数才能读取整个文件(通常是 2-4),然后遍历一个 for 循环读取文件(忽略“ +17+filenamesize
”的东西,这都是以后需要的该程序。
但是,只有在第一次时j=1
,它才会真正将数据放入buf
数组中。在其他情况下,当j != 1
,strlen(buf)
返回0。
fseek()
我认为问题在于在读取文件之前使用寻找文件的第二部分或内存分配问题。
任何帮助将不胜感激让它将文件的1000-1999
第 th 个字符读入buf
数组。
附上代码的相关部分:
unsigned char *buf;
source = fopen(localpath,"r");
temp = filesize / BLOCK_SIZE + 1;
for (j=1; j <= temp; j++) {
if (j == 1) {
buf = (unsigned char *) malloc((sizeof(unsigned char)) * (BLOCK_SIZE + 17 + filenamesize));
fread(buf+17+filenamesize, sizeof(unsigned char), BLOCK_SIZE, source);
} else if (j == temp) {
buf = (unsigned char *) malloc((sizeof(unsigned char)) * (filesize + 5 - BLOCK_SIZE*(j-1)));
fseek(source, BLOCK_SIZE*(j-1), SEEK_SET); // off by one warning
fread(buf+5, sizeof(unsigned char), filesize - BLOCK_SIZE*(j-1), source);
} else {
buf = (unsigned char *) malloc((sizeof(unsigned char)) * (5+BLOCK_SIZE*(j-1)));
fseek(source, BLOCK_SIZE*(j-1), SEEK_SET); // off by one warning
fread(buf+5, sizeof(unsigned char), BLOCK_SIZE, source);
}
// do stuff with buf here
buf = "";
free(buf);
}