Linux 文件操作
2017-06-24 16:23
148 查看
我们都知道linux不以后缀名识别文件,通过inode标识文件,通过文件描述符fd来操作文件。linux 内核维护一个已打开文件的记录表。有两个例子作为文件信息的学习总结。深度优先遍历目录下所有文件;通过stat函数模仿ls -l的效果展示
再写一点文件操作的知识点
1.深度优先搜索寻找文件夹下所有文件
2.通过stat函数模仿展示ls -l效果
包括文件类型、权限、链接数、uid、gid、size、date、filename
再写一点文件操作的知识点
1.int fd=open (filename,flags,mode_t) //flags一般写O_RDWR|O_CREAT|O_TRUNC,按照读写方式打开,文件不存在则创建,文件存在则将其长度置为0.mode_t 权限,前面内容说过了,比如可以直接赋值755. 2.DIR *opendir(const char *name); //打开一个目录 struct dirent *readdir(DIR *dir); //读取目录的一项信息,并返回该项信息的结构体指针
1.深度优先搜索寻找文件夹下所有文件
#include<stdio.h> #include<dirent.h> #include<string.h> void printdir(char *path,int width){ DIR *dir= opendir(path); struct dirent *entry; char buf[512]; while((entry=readdir(dir))!=NULL){ // 文件名称不是 . 或者 .. if((strcmp(entry->d_name,"."))&&(strcmp(entry->d_name,".."))){ memset(buf,0,sizeof(buf)); //每行输出width长度的空格 printf("%*s%s\n",width,"",entry->d_name); if(entry->d_type==4){ //拼接子目录的路径 sprintf(buf,"%s%s%s",path,"/",entry->d_name); printdir(buf,width+4); } } } } int main(int argc,char *argv[]){ printdir(argv[1],0); return 0; }
2.通过stat函数模仿展示ls -l效果
包括文件类型、权限、链接数、uid、gid、size、date、filename
#include <sys/types.h> #include <dirent.h> #include <stdio.h> #include<stdlib.h> #include <time.h> #include <sys/stat.h> #include <unistd.h> #include <string.h> #include <pwd.h> #include <grp.h> //最新的时间 char* printdate(char *result,char *old){ int e=snprintf(result,17*sizeof(char),"%s",old); //printf("get: %s\n",result); return result+4; } //最新的文件权限 void printmode(mode_t st_mode){ if(S_ISDIR(st_mode)) printf("d"); else if (S_ISREG(st_mode)) printf("-"); else if (S_ISCHR(st_mode)) printf("c"); else if (S_ISBLK(st_mode)) printf("b"); else if (S_ISFIFO(st_mode)) printf("p"); else if (S_ISLNK(st_mode)) printf("l"); if(st_mode&S_IRUSR) printf("r");else printf("-"); if(st_mode&S_IWUSR) printf("w");else printf("-"); if(st_mode&S_IRUSR) printf("x");else printf("-"); if(st_mode&S_IXUSR) printf("r");else printf("-"); if(st_mode&S_IRGRP) printf("w");else printf("-"); if(st_mode&S_IWGRP) printf("x");else printf("-"); if(st_mode&S_IROTH) printf("r");else printf("-"); if(st_mode&S_IWOTH) printf("w");else printf("-"); if(st_mode&S_IXOTH) printf("x");else printf("-"); } int main(int argc,char* argv[]) { DIR *dir; dir=opendir(argv[1]); struct dirent *p; struct stat buf; int ret; char path[512]; char result[100]; //保存更改后的时间 while((p=readdir(dir))!=NULL) { if(strcmp(p->d_name,".")&&strcmp(p->d_name,"..")) { memset(&buf,0,sizeof(buf)); memset(path,0,sizeof(path)); memset(result,0,sizeof(result)); //p是dirent结构指针,&buf是stat结构指针 //拼接目录下文件的路径 sprintf(path,"%s%s%s",argv[1],"/",p->d_name); ret=stat(path,&buf); char *time=printdate(result,ctime(&buf.st_mtime)); printmode(buf.st_mode); printf(" %d %s %s %5ld %s %s\n",buf.st_nlink,getpwuid(buf.st_uid)->pw_name,getgrgid(buf.st_gid)->gr_name,buf.st_size,time,p->d_name); } } closedir(dir); return 0; }
相关文章推荐
- Linux下文件的操作
- c++ 操作.ini 文件(适用平台linux windows)
- Linux实用代码(1)--文件系统操作
- Linux下文件操作(暨体系实习2小程序)
- linux程序设计入门--文件操作:linux下文件的操作
- Linux/Unix 下对文件操作的学习
- Linux下C语言编程--文件的操作
- Linux命令学习(三):文件操作命令(1)
- linux下c语言编程:文件操作
- Linux下C语言编程--文件的操作
- Linux 基本命令(文件操作)
- linux的文件操作
- 如何在linux下使用c语言操作临时文件
- linux操作系统下c语言编程入门 -- (3)文件操作
- linux下的文件操作(1)
- linux实验报告二--文件操作
- linux实验报告二--文件操作(续)
- Linux下C语言编程--文件的操作
- linux 文件操作函数 通过例子来解释 父子间文件描述符共享 内存映像图
- linux程序设计第三章 文件 目录操作读书笔记