Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我必须读取一个 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++; }
但没有运气
您永远不应该使用文件库读取二进制文件,而应该使用系统调用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和此处。_lseek
lseek