文件和目录
来源:互联网 发布:冷烟花在淘宝上怎么买 编辑:程序博客网 时间:2024/06/06 11:37
学习笔记,小白可以相互学习,大佬看到能告诉咱理解不对的地方就好了。
函数
1.stat
获取文件/目录属性信息
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *path, struct stat *buf); //主要掌握这一个,函数返回一个与此命名有关的信息结构
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
struct passwd *uid = getpwuid(b.st_uid); //获得用户名
struct group *gid = getgrgid(b.st_gid);//获得所属组名
struct tm *t = localtime(&b.st_ctime);//获取时间
printf("%2d月,%2d日,%2d点,%2d分,%2d秒 ",t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
/**************自己写的实现ls -l功能*******************************/
#include<stdio.h>#include<fcntl.h>#include<sys/stat.h>#include<sys/types.h>#include<unistd.h>#include<dirent.h>#include<pwd.h>#include<grp.h>#include<time.h>int main(){ DIR *f = opendir("."); struct dirent *r; struct stat b; while( r = readdir(f) ) { if( 0 > stat(r->d_name,&b)) { perror("stat"); return -1; } struct passwd *uid = getpwuid(b.st_uid); //获得用户名 struct group *gid = getgrgid(b.st_gid);//获得所属组名 printf("userID:%s ",uid->pw_name); printf("groupID:%s ",gid->gr_name); //******************文件类型************** switch(b.st_mode & S_IFMT)//文件类型的位遮罩S_TFMT { case S_IFREG: printf("-"); break; case S_IFDIR: printf("d"); break; case S_IFCHR: printf("c"); break; case S_IFBLK: printf("b"); break; case S_IFIFO: printf("p"); break; case S_IFLNK: printf("c"); break; case S_IFSOCK: printf("s"); break; }//***********************文件的权限************** (b.st_mode & S_IRUSR?printf("r"):printf("-")); (b.st_mode & S_IWUSR?printf("w"):printf("-")); (b.st_mode & S_IXUSR?printf("x"):printf("-")); (b.st_mode & S_IRGRP?printf("r"):printf("-")); (b.st_mode & S_IWGRP?printf("w"):printf("-")); (b.st_mode & S_IXGRP?printf("x"):printf("-")); (b.st_mode & S_IROTH?printf("r"):printf("-")); (b.st_mode & S_IWOTH?printf("w"):printf("-")); (b.st_mode & S_IXOTH?printf("x "):printf("- ")); //**********文件大小************************ printf("%5ld ",b.st_size); //************时间******************************* struct tm *t = localtime(&b.st_ctime); printf("%2d月,%2d日,%2d点,%2d分,%2d秒 ",t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);//************文件名字*************** printf("%s",r->d_name); printf("\n"); } closedir(f); return 0;}
/*******************标准点的********************************/
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <strings.h>#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>#include <pwd.h>#include <grp.h>#include <time.h>void getfiletype(mode_t m, char *type){if (S_ISREG(m)) *type = '-';else if (S_ISDIR(m))*type = 'd';else if (S_ISCHR(m))*type = 'c';else if (S_ISBLK(m))*type = 'b';else if (S_ISFIFO(m))*type = 'p';else if (S_ISLNK(m))*type = 'l';else if (S_ISSOCK(m))*type = 's';}void getfilemode(mode_t m, char *mode){if (m & S_IRUSR)mode[0] = 'r';if (m & S_IWUSR)mode[1] = 'w';if (m & S_IXUSR)mode[2] = 'x';if (m & S_IRGRP)mode[3] = 'r';if (m & S_IWGRP)mode[4] = 'w';if (m & S_IXGRP)mode[5] = 'x';if (m & S_IROTH)mode[6] = 'r';if (m & S_IWOTH)mode[7] = 'w';if (m & S_IXOTH)mode[8] = 'x';}void getfileusrname(uid_t uid, char *usrname){struct passwd *pw = getpwuid(uid);strcpy(usrname, pw->pw_name);}void getfilegrpname(gid_t gid, char *grpname){struct group *grp = getgrgid(gid);strcpy(grpname, grp->gr_name);}void getfiletime(time_t ctime, char *filetime){struct tm *tm = localtime(&ctime);sprintf(filetime, "%2d月 %2d %2d:%2d", tm->tm_mon+1, tm->tm_mday, \tm->tm_hour, tm->tm_min);}int main(){DIR *dirp = opendir(".");struct dirent *file;struct stat buf;while (file = readdir(dirp)) {if (0 > stat(file->d_name, &buf)) {perror("stat");break;}char type;getfiletype(buf.st_mode, &type);char mode[9] = {0};memset(mode, '-', sizeof(mode));getfilemode(buf.st_mode, mode);char usrname[10];getfileusrname(buf.st_uid, usrname);char grpname[10];getfilegrpname(buf.st_gid, grpname);char filetime[20];getfiletime(buf.st_ctime, filetime);fprintf(stdout, "%c%9s %2d %10s %10s %6ld %15s %s\n", type, mode, buf.st_nlink, usrname, grpname, buf.st_size, filetime, file->d_name);}return 0;}
2.opendir
打开目录
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
成功返回DIR*类型的目录流,失败返回NULL
3.readdir
读取目录
#include <dirent.h>
struct dirent *readdir(DIR *dirp);
成功返回下个目录进入点,失败或者到达目录文件尾返回NULL
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all file system types */
char d_name[256]; /* filename */
};
DIR *f = opendir(".");//打开当前目录
struct dirent *r;
struct stat b;
while(r = readdir(f)) //读取目录
{printf("%s",r->d_name);}//目录下的成员名
4.closedir
关闭目录
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
关闭成功返回0,失败返回-1,并设置errno
- 文件和目录
- 创建文件和目录
- 删除文件和目录
- 文件和目录操作
- 文件和目录
- symbian文件和目录
- 文件和目录
- 文件和目录操作
- 文件和目录管理
- 文件和目录操作
- Linux 文件和目录
- symbian文件和目录
- C#文件和目录
- linux文件和目录
- 文件和目录
- 文件和目录
- 遍历文件和目录
- 文件和目录
- TensorFlow1.0 运行之前版本代码报错解决
- node.js安装(转载)
- java基础练习题
- 二叉排序树、平衡二叉树以及红黑树
- POJ 2.5 8783 && luogu p1019 单词接龙
- 文件和目录
- 前端面试题目合集
- hdu1159 最大公共子序列(lcs) hdu 1080
- ng2学习——简单后台demo的实现
- [ poj 3616] Milking Time [ DP ]
- BOOST库 之 string_algo 字符串的增删改查
- mysql
- greenDao的简单使用教程
- LintCode407