int main(int argc, const char *argv[])
{
int fd = -1;
if((fd = open("./GGbond.bmp", O_RDWR|O_CREAT|O_APPEND, 0664)) == -1)
{
perror("open error");
return -1;
}
//读取该图片的大小,需要将光标向后偏移2个字节
lseek(fd, 2, SEEK_SET);
int bmpsize = 0;
//读取出图片的大小
bmpsize = lseek(fd, 0, SEEK_END);
printf("bmpsize = %d\n", bmpsize);
char buf[bmpsize];
//前54字节是文件头和信息头所占的空间
//想要对图像内容进行读写操作
//需要将光标向后偏移
lseek(fd, 54, SEEK_SET);
int res;
unsigned char color[3] = {0, 255, 0};
for(int i=0; i<200; i++)
{
for(int j=0; j<900; j++)
{
write(fd, color, sizeof(color));
}
}
close(fd);
return 0;
}
运行现象:
#include <myhead.h>
int main(int argc, const char *argv[])
{
//定义一个目录
DIR *dp = NULL;
//打开目录
if((dp = opendir("./")) == NULL)
{
perror("opendir error");
return -1;
}
//定义一个文件指针,用于存储读取的文件信息
struct dirent *rp = NULL;
//定义时间结构体
time_t sys_time = 0;
sys_time = time(NULL);
struct tm *tp;
//定义一个状态结构体
struct stat st;
//循环读取当前目录下的所有文件
printf("文件权限 硬链接 用户ID 组ID 文件大小 月份 日期 时间 文件名\n");
while((rp = readdir(dp)) != NULL)
{
tp= localtime(&sys_time);
//将当前文集你的信息打印出来
printf("%#o\t\t%d\t%d\t%ld\t%d\t%d\t%d\t %02d:%02d\t%s\n",st.st_mode&0777, st.st_nlink, st.st_uid, st.st_gid, st.st_size,\
tp->tm_mon+1,tp->tm_mday, tp->tm_hour, tp->tm_min, rp->d_name);
}
//关闭目录
closedir(dp);
return 0;
}
运行现象
思维导图: