用fseek读取文件的某个部分

时间:2013-04-07 19:39:17

标签: file fseek

我必须读取一个png文件(二进制),我必须得出图片的高度和宽度(从17开始,长8字节一起)。

我试过

FILE* picture = fopen("test.png","rb");
fseek(picture, 16 , SEEK_SET);

int counter = 0;
while(counter < 8){
    fscanf(picture, "%d", temp[counter]);
    counter++;
}

但没有运气

1 个答案:

答案 0 :(得分:0)

您永远不应该使用文件库读取二进制文件,而是系统调用readwrite

void* buf = malloc(1024);
memset(buf,0,1024);

int picturefd = open("test.png",O_RDONLY);
lseek(picturefd, 16 , SEEK_SET);

if(read(picturefd, buf, 8) < 8){
//failed to read or eof reached
}

readherelseekhere