标签: 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++; }
但没有运气
答案 0 :(得分:0)
您永远不应该使用文件库读取二进制文件,而是系统调用read和write。
read
write
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 }
read见here,lseek见here。
lseek