您的位置:首页 > 运维架构 > Linux

Linux 文件 操作

2017-01-21 22:46 363 查看

Linux file 操作

目录结构体

struct dirent{
ino_t
...

}

struct dirent


inode

proc inode 不一样

目录遍历

off_t pos

readdir // 读取目录信息

打印目录树

打开目录 readdir 判断文件是否为目录d_type==4 考虑. ..

recursive 递归

#include<sys/types.h>
#include<dirent.h>
#include<stido.h>
void printdir(char *path,int width) // 打印路径下的文件,width控制格式
{
DIR* dir; // define dir
dir=opendir(path); // open dir
if(NULL==dir)  // dir true or false
{
perror("opendir");
return;
}
struct dirent *p; // 目录结构
char buf[200]={0};
while(   (p=readdir(dir))!=NULL )  //考虑当前目录,不然死循环
{   if((strcmp(p->d_name,".")||strcmp(p->d_name,"..")){
// continue

}else{
printf("%*s%s\n",width,"",p->d_name); //按层及目录打印  // p->d_name;
if(p->d_type==4)  // 4
{
sprintf(buf,"%s%s%s",path,"/",p->d_name);     // 拼接
printdir(buf,width+4);
}
}
}
}

//
int main(int argc,char *argv[])
{if(argc!=2){
printf("error args!!!,check your args");
return -1 ;

}
printf("%s\n",argv[1]);
printdir(argv[1],0);
return 0;
}

< 不错,今天调整的变为平静,空间、世界、love、do、change


软链接:存的是路径

硬连接:iNode 入口

// ls -l

unistd.h
stdio.h
...
int ret=0;

int main()
{
if(argc!=2){
printf("error");
}

}
struct stat buf;  // use struct stat
memset(&buf,0,sizeof(buf)); // iniciaze

ret=stat(argv[1],&buf);
if // ret ==-1

// for dir

printf("mode=%d"  // buf.st_mode ...

// getpwuid()


type 文件类型是单独存储的

getpwuid

think how to 81b4 to -rw-rw-r– ls -l

for(){  // number to char function

}


标准输入、输出

基于描述

创建文件

main

// 创建

// open(const *path, int flags,mode_t mode)

open(argv[1],O_RDONLY|O_WRONLY,666);

I find life an exciting business and most exciting when it is lived for others.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: