用FREAD()和fseek的读取文件的一部分,C() [英] Reading part of a file in C using fread() and fseek()
问题描述
我想文件读入缓冲区的大小 BLOCK_SIZE
(目前相等块 1000无符号字符
)。我的code最初是通过一个发现的块数,将必须按顺序读取整个文件(通常2-4)读取,然后遍历for循环读取文件(忽略了 + 17 + filenamesize
的东西,这是所有需要在节目后。
然而,仅在第一次,当 J = 1
,它实际上把数据放到 BUF
数组。在其他情况下,当 J 1 = 1
,的strlen(BUF)
收益 0 。
我认为这个问题是不是与使用 fseek的()
阅读它,或内存分配问题之前寻求到一个文件中的第二部分。
任何帮助将是AP preciated得到它来读取文件中的 1000-1999
个字符到 BUF
阵列。
附件是code的相关部分:
无符号字符* BUF;
来源= FOPEN(localPath来,R);
TEMP =文件大小/ BLOCK_SIZE + 1;为(J = 1; J< =温度; J ++){
如果(J == 1){
BUF =(无符号字符*)malloc的((sizeof的(无符号字符))*(BLOCK_SIZE + 17 + filenamesize));
FREAD(BUF + 17 + filenamesize,sizeof的(无符号字符),BLOCK_SIZE,源);
}否则如果(J ==临时){
BUF =(无符号字符*)malloc的((sizeof的(无符号字符))*(文件大小+ 5 - BLOCK_SIZE *(J-1)));
fseek的(源,BLOCK_SIZE *(J-1),SEEK_SET);一个警告//关闭
FREAD(BUF + 5的sizeof(无符号字符),文件大小 - BLOCK_SIZE *(J-1),源);
}其他{
BUF =(无符号字符*)malloc的((sizeof的(无符号字符))*(5 + BLOCK_SIZE *(J-1)));
fseek的(源,BLOCK_SIZE *(J-1),SEEK_SET);一个警告//关闭
FREAD(BUF + 5的sizeof(无符号字符),BLOCK_SIZE,源);
}
//做的东西跟这里BUF BUF =;
免费(BUF);
}
我会建议您检查的 fseek的和 FREAD 。特别是,要确保fseek的是返回0 - 如果不是,这可能是整个问题
只要fseek的正在取得成功,FREAD应该告诉你读取的字节总数。
此外,strlen的是不是一定要使用有效的东西,因为它会假设这是一个空结尾的字符串。如果你读了第一个字符是0字节,strlen的将返回0。你不是这当作一个空结尾的字符串(你是不是对德空终止分配足够的空间 - 恰好是相反的,以满足您的二进制数据需要些什么) ,所以strlen的可能是不合适的。
I'm trying to read a file into a buffer in blocks of size BLOCK_SIZE
(currently equal to 1000 unsigned chars
). My code initially finds the number of blocks it will have to read in order to read the entire file (usually 2-4), then iterates through a for loop reading the file (ignore the "+17+filenamesize
" stuff, that is all needed for later in the program.
However, only on the first time, when j=1
, does it actually put data into the buf
array. In other cases, when j != 1
, strlen(buf)
returns 0.
I think the problem is either with the use of fseek()
to seek to the second part of a file before reading it or a memory allocation issue.
Any help would be appreciated for getting it to read the 1000-1999
th chars of the file into the buf
array.
Attached is the relevant part of the code:
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);
}
I would recommend checking the results of fseek and fread. In particular, make sure fseek is returning 0 - if it's not, this may be the entire problem.
Provided that fseek is succeeding, fread should tell you the total number of bytes read.
Also, strlen is not necessarily a valid thing to use, since it's going to assume that this is a null terminated string. If the first character you read is a 0 byte, strlen will return 0. You're not treating this as a null terminated string (you aren't allocating enough space for teh null terminator - just exactly what's needed to fit your binary data), so strlen is probably inappropriate.
这篇关于用FREAD()和fseek的读取文件的一部分,C()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!