1. 使用文件IO完成对图像的读写操作

代码

#include<myhead.h>

int main(int argc, const char *argv[])
{
    int fd=-1;

    if((fd = open("./bird.bmp", O_RDWR)) == -1)
    {
        perror("fopen error");
        return -1;
    }

    //读取该图片的大小,需要将光标向后偏移2个字节
    lseek(fd, 2, SEEK_SET);
    int bmpsize = 0;

    //读取出图片的大小
    read(fd, &bmpsize, sizeof(int));
    printf("bmpsize = %d\n", bmpsize);

    //前54字节是文件头和信息头所占空间
    //想要对图像内容进行读写操作
    //需要将光标向后偏移54字节后,再进行操作
    lseek(fd, 54, SEEK_SET);

    unsigned char color[3] = {255, 255, 255};      //定义一个颜色,白色
    for(int i=0; i<65; i++)     //外行
    {
        for(int j=0; j<1200; j++)   //内列
        {
			for (int k=0; k<3; k++)
			{

			char c;
			read(fd, &c, sizeof(char));
			c=(c+32)%255;
            write(fd, &c, sizeof(char));
			
			
			}
        }
    }

    //关闭文件
    close(fd);

    return 0;
}

原图片

IO / day03 作业-LMLPHP

修改后图片

IO / day03 作业-LMLPHP

2. 使用stat函数实现 ls -l 指令功能

代码

#include <myhead.h>

int main(int argc, const char *argv[])
{

	//1.define dir ptr
	DIR *dptr = NULL;

	//2.open directory
	if( (dptr=opendir("./") )==NULL )
	{
		perror("opendir error ");

	}

	//3. define file ptr
	struct dirent *fptr = NULL;



	//定义文件状态结构体
	struct stat st;
	size_t size;
	struct passwd *pwd=NULL;
	struct group *grp=NULL;


	//4.read files 
	while((fptr=readdir(dptr))!=NULL)
	{
		char filename[256]="./";
		strcat(filename, fptr->d_name);

		//printf("filename=%s\n", filename);

		//定义文件状态结构体
		struct stat st;

		//获取信息存放在结构体变量st里
		if( (stat(filename, &st))==-1 )
		{
			perror("stat error");
			return -1;
		}

		//file type
		switch (st.st_mode & S_IFMT) {
		case S_IFBLK:  printf("b"); break;
		case S_IFCHR:  printf("c"); break;
		case S_IFDIR:  printf("d"); break;
		case S_IFIFO:  printf("p"); break;
		case S_IFLNK:  printf("l"); break;
		case S_IFREG:  printf("-"); break;
		case S_IFSOCK: printf("s"); break;
		default:       printf("unknown?\n"); break;
		}

		//file access
		int au=st.st_mode&0777;	
		//printf("au=%o\n", au);
		char crwx[9]="";
		char tmp[9]="";
		for(int i=0; i<3; i++)
		{
			for(int j=1; j<=3; j++)
			{
				char bit =(char) au&(01);
				if(bit==0)
				{
					strcpy(tmp, "-");
					strcat(tmp, crwx);
					strcpy(crwx, tmp);
				}
				else if(bit==1 && (j%3)==1 )
				{
					strcpy(tmp, "x");

					strcat(tmp, crwx);
					strcpy(crwx, tmp);

				}
				else if(bit==1 && (j%3)==2 )
				{
					strcpy(tmp, "w");
					strcat(tmp, crwx);
					strcpy(crwx, tmp);

				}
				else if(bit==1 && (j%3)==0 )
				{
					strcpy(tmp, "r");
					strcat(tmp, crwx);
					strcpy(crwx, tmp);

				}


				au=au>>1;

			}		

		}
		printf("%s ", crwx);

		printf("%ld ", st.st_nlink);
		
		pwd=getpwuid(st.st_uid);
		printf("%s ", pwd->pw_name);

		grp=getgrgid(st.st_uid);
		printf("%s ", grp->gr_name);

		printf("%10ld ", st.st_size);

		struct tm *at = localtime(&(st.st_atime));
		printf("%2d %2d %02d:%02d:%02d ", at->tm_mon+1, at->tm_mday, at->tm_hour, at->tm_min, at->tm_sec);

		printf("%s\n", fptr->d_name);

	}

	return 0;
}

运行结果

ubuntu@ubuntu:~/embedded/04_io/day03$ ./a.out
drwxrwxr-x 5 ubuntu ubuntu       4096 12  5 17:12:30 ..
-rwxrwxr-x 1 ubuntu ubuntu      13000 12  5 21:01:30 a.out
-rw-rw-r-- 1 ubuntu ubuntu         77 12  5 15:36:06 11_dup.c
-rwxrwxrwx 1 ubuntu ubuntu         14 12  5 14:14:42 test.txt
-rw-rw-r-- 1 ubuntu ubuntu         14 12  5 14:29:39 test.bk
-rw------- 1 ubuntu ubuntu    2844054 12  5 20:58:46 bird_r.bmp
-rw-rw-r-- 1 ubuntu ubuntu        400 12  5 11:42:12 05_umask.c
-rw-rw-r-- 1 ubuntu ubuntu        240 12  5 10:46:58 02_open.c
-rw-rw-r-- 1 ubuntu ubuntu         91 12  5 14:48:38 09_lseek.c
-rw-rw-r-- 1 ubuntu ubuntu        109 12  5 15:02:35 10_fd_cpy.c
-rw-rw-r-- 1 ubuntu ubuntu       1007 12  5 20:53:17 12_hw_read_write.c
-rw-rw-r-- 1 ubuntu ubuntu        215 12  5 09:42:22 01_file_desc.c
-rw-rw-r-- 1 ubuntu ubuntu       2139 12  5 21:00:37 14_hw_stat.c
drwxrwxr-x 2 ubuntu ubuntu       4096 12  5 21:01:30 .
-rw-rw-r-- 1 ubuntu ubuntu        910 12  5 17:20:39 13_stat.c
-rw-rw-r-- 1 ubuntu ubuntu        527 12  5 13:59:38 07_read_write.c
-rw-rw-r-- 1 ubuntu ubuntu        385 12  5 11:33:14 04_open_excl.c
-rw-rw-r-- 1 ubuntu ubuntu        734 12  5 14:32:39 08_cpy.c
-rw-rw-r-- 1 ubuntu ubuntu        594 12  5 20:47:33 06_close.c
-rw-rw-r-- 1 ubuntu ubuntu        247 12  5 11:28:57 03_open.c
12-07 11:07