思维导图
练习:
1.使用文件IO读取图片 文件大小、文件偏移量,宽度,高度
#include <head.h>
int main(int argc, const char *argv[])
{int fd=open("/home/ubuntu/3.5/xiaoxin.bmp",O_RDONLY);if(fd==-1)PRINT_ERROR("open error");//大小int size;printf("fd=%d\n",fd);lseek(fd,2,SEEK_SET);if(read(fd,&size,sizeof(size))<=0){PRINT_ERROR("read error");return -1;}printf("文件大小为%d\n",size);//文件偏移量lseek(fd,4,SEEK_CUR);int res;if(read(fd,&res,4)<=0){PRINT_ERROR("read error");}printf("偏移量大小为%d\n",res);//宽lseek(fd,4,SEEK_CUR);int wide;read(fd,&wide,4);printf("文件宽为:%d\n",wide);//高lseek(fd,0,SEEK_CUR);int h;read(fd,&h,4);printf("文件高为:%d\n",h);close(fd);return 0;
}
2.向一个程序中输入文件名,判断指定目录下是否有这个文件,如果有这个文件,
将这个文件的属性信息输出。如果不存在输出不存在即可。
#include <head.h>
int main(int argc, const char *argv[])
{const char * dirname=argv[1];const char * filename=argv[2];DIR *d=opendir(dirname);if(d==NULL)PRINT_ERROR("opendir error");struct dirent *entry;struct stat file_stat;char file_path[1024];int found=0;while ((entry = readdir(d)) != NULL) {if (strcmp(entry->d_name, filename) == 0) {snprintf(file_path, sizeof(file_path), "%s/%s", dirname, filename);if (stat(file_path, &file_stat) == -1) {PRINT_ERROR("获取文件信息失败");}printf("文件存在,属性信息如下:\n");printf("文件路径: %s\n", file_path);printf("文件大小: %ld 字节\n", file_stat.st_size);printf("文件权限: %o\n", file_stat.st_mode & 0777);printf("文件硬链接数: %ld\n", file_stat.st_nlink);struct tm *tm_info = localtime(&file_stat.st_mtime);if (tm_info == NULL){PRINT_ERROR("localtime error");}printf("最后修改的时间:%d-%d-%d %d:%d:%d\n",tm_info->tm_year+1900,tm_info->tm_mon+1,tm_info->tm_mday,tm_info->tm_hour,tm_info->tm_min,tm_info->tm_sec);found = 1;break;}}if(!found){printf("文件不存在\n");}if(closedir(d)==-1)PRINT_ERROR("closedir error");return 0;
}