阶段小项目2:显示bin格式图片

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<error.h>
#include<errno.h>
#include<unistd.h>
#include<strings.h>
#include<stdbool.h>

#include<sys/stat.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<sys/mman.h>
int main(int argc,char **argv)
{
if(argc != 2)
{
fprintf(stderr,"Usage:%s <dir>",argv[0],strerror(errno));
}
int fd = open("/dev/fb0",O_RDWR);
if(fd == -1)
{
perror("open() failed!");
exit(0);
}

int fd0 = open(argv[1],O_RDONLY);//注意bin文件是一个只读文件否则无法打开
if(fd0 == -1)
{
perror("open fd0 failed !");
exit(0);
}
char *p = mmap(NULL,800*480*4,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);

int nread = 800*480*4;
int n;
while(nread > 0)
{
n = read(fd0,p,nread);//可以直接读到映射内存中,中间不需要一个buff.
nread -= n;   //这里不能确定是否能一次全部读取到。
p += n; //注意指针的移动。
}
return 0;
}

原文地址:https://www.cnblogs.com/defen/p/5189514.html