1.stat(1)
2.man -k ,grep -r的使用
3.伪代码
判断输入中是否包含文件参数——若有,则继续。
——若没有,则提示用户输入错误。
然后声明结构体,并调用stat()函数给结构体复制,将文件的设备编号、节点、文件的类型和存取的权限、镰刀该文件的硬链接数目等按顺序输出。
4.产品代码mystat.c,提交码云链接
#include <sys/types.h> #include <sys/stat.h> #include <time.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { struct stat stat1; if (argc != 2) { fprintf(stderr, "Usage: %s <pathname>\n", argv[0]); exit(EXIT_FAILURE); } if (stat(argv[1], &stat1) == -1) { perror("stat"); exit(EXIT_FAILURE); } printf("文件: %s\n",argv[1]); printf("Inode: %ld\n", (long) stat1.st_ino); printf("硬链接: %ld\n", (long) stat1.st_nlink); printf("权限: Uid = %ld Gid = %ld\n",(long) stat1.st_uid, (long) stat1.st_gid); printf("IO块: %ld ",(long) stat1.st_blksize); switch (stat1.st_mode & S_IFMT) { case S_IFBLK: printf("块设备\n"); break; case S_IFCHR: printf("character device\n"); break; case S_IFDIR: printf("目录\n"); break; case S_IFIFO: printf("FIFO/管道\n"); break; case S_IFLNK: printf("符号链接\n"); break; case S_IFREG: printf("普通文件\n"); break; case S_IFSOCK: printf("socket\n"); break; default: printf("未知?\n"); break; } printf("大小: %lld bytes\n",(long long) stat1.st_size); printf("块: %lld\n",(long long) stat1.st_blocks); printf("最近访问: %s", ctime(&stat1.st_atime)); printf("最近更改: %s", ctime(&stat1.st_mtime)); printf("最近改动: %s", ctime(&stat1.st_ctime)); exit(EXIT_SUCCESS); }
5.测试代码,mystat与stat(1)对比,提交截图
标签:case,stat1,long,st,break,mystat,printf From: https://www.cnblogs.com/syf0105/p/16795291.html