文件目录,遍历文件夹文件及属性

版权声明:请在征得作者同意的情况下,可以进行非盈利性引用。引用请注明出处:“作者:慕华思弦 转载地址” 字样,以尊重作者的劳动成果,并保持良好的版权意识。 https://blog.csdn.net/Superman___007/article/details/82754231

目录文件:
    1、创建目录:
        注:目录需要执行的权限,创建文件夹mode& ~umask
         
    2、读取目录的内容:子文件
        打开目录
            opendir
        读取目录
            struct dirent *readdir(DIR *dirp);
        关闭目录
            closedir

   

   创建一个文件夹,并设置文件夹的权限 , mkdir(argv[1],0757)  的第二个参数是八进制用来设置权限的,例如要设置  0755 的权限的话,则需要传入 0757 的值 .

   0757(mode) 的由来 : 首先在shell命令下输入 umask ,查看当前的 umask为多少 , 我的 umask=0002 ; 

                          mode & ~umask  = 权限 

                          mode & ~0002   =  0755        (这里取反是 0对应7 , 2对应5)

                          mode  -  0002    =  0755        (按位 "& ~" 等价于 相减)

                          mode  = 0757 

#include<iostream>
#include<sys/types.h>
#include<sys/stat.h>
using namespace std;
int main(int argc,char* argv[])
{
	if(argc<2)
	{
		cout<<"参数不足"<<endl;
		return -1;
	}
	//调用系统的API:mkdir 创建文件夹(创建者7  群5 其5)
	mkdir(argv[1],0757);//mode - umask   755  
	return 0;
}

   打开文件夹  DIR* dir=opendir("/home/LF/0907/aaa/ccc");

   读取文件夹文件  (pd=readdir(dir))!=NULL  判断不为空 .

   读取文件的名字  pd->d_name .

   判断文件的类型  pd->d_type .

   关闭操作  closedir(dir);

#include<iostream>
#include<sys/types.h>
#include<dirent.h>
using namespace std;
int main()
{
//1打开文件夹
	DIR* dir=opendir("/home/LF/0907/aaa/ccc");
	if(NULL==dir)
	{
		cout<<"open fail"<<endl;
	}
//2操作--读,偏移
	struct dirent *pd=NULL;
	while((pd=readdir(dir))!=NULL)
	{
		cout<<pd->d_name;
		switch(pd->d_type)
		{
			case DT_DIR:
				cout<<"是文件夹"<<endl;
			break;
			case DT_REG:
				cout<<"是普通文件"<<endl;
			break;
			default:
				cout<<"其他文件"<<endl;
		}
	}
//3关闭
	closedir(dir);
	return 0;
}

   递归来遍历 , 遍历路径下所有文件夹和文件 .

#include<iostream>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
using namespace std;
//给出一个路径,只遍历出当前路径的内容。
void list(const char* path)
{
//1打开文件夹
	DIR* dir=NULL;
	dir=opendir(path);
	if(NULL==dir)
		return;
//2操作
	struct dirent* pd=NULL;
	while((pd=readdir(dir))!=NULL)
	{
		if(!strcmp(pd->d_name,".") || !strcmp(pd->d_name,"..") )
			continue;
		cout<<pd->d_name<<endl;
		//判断是否是文件夹,构成新的路径并重亲遍历
		if(pd->d_type==DT_DIR)
		{
			char cpath[256]="";
			strcat(cpath,path);//父路径
			strcat(cpath,"/");//分隔符
			strcat(cpath,pd->d_name);//子文件夹名
			list(cpath);//
		}
	}
//3关闭
	closedir(dir);
}
int main()
{
	list("/home/LF/0917");

	return 0;
}

   实现遍历当前路径下所有文件及文件夹的文件属性 .

#include<iostream>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<string.h>
#include<dirent.h>
using namespace std;
//ls 路径
//ls -l 路径
//打印某进制位
void show(short x)//每一个权限位
{
	switch(x)
	{
		case 0:
			cout<<"---";
		break;
		case 1:
			cout<<"--x";
		break;
		case 2:
			cout<<"-w-";
		break;
		case 3:
			cout<<"-wx";
		break;
		case 4:
			cout<<"r--";
		break;
		case 5:
			cout<<"r-x";
		break;
		case 6:
			cout<<"rw-";
		break;
		case 7:
			cout<<"rwx";
		break;
	}
}

void list(char* argv)
{
	struct stat st;
	if(0==stat(argv,&st))
	{
		//打印文件的类型
		switch(st.st_mode&0170000)
		{
			case S_IFSOCK:		//套接字
				cout<<'s';
			break;
			case S_IFDIR://文件
				cout<<'d';
			break;
			case S_IFLNK:	//符号
				cout<<'l';
			break;
			case S_IFBLK://块设备
				cout<<'b';
			break;
			case S_IFIFO://管道
				cout<<'f';
			break;
			case S_IFCHR:	//字符
				cout<<'c';
			break;
			case S_IFREG:	//普通
				cout<<'-';
			break;
		}
		//文件权限
		show((st.st_mode&0000700)>>6);//所有者
		show((st.st_mode&0000070)>>3);//群组
		show(st.st_mode&0000007); //其他人
		cout<<'.';
		cout<<" "<<st.st_nlink<<" ";//链接数量
		struct tm* t;
		t=gmtime(&st.st_mtime);//获取时间并转换
		cout<<t->tm_mon<<"月 "<<t->tm_mday<<"日 "<<t->tm_hour<<":"<<t->tm_min<<" ";
		//cout<<argv;//名字
		cout<<endl;
	}
}

void show2(char name[])
{
	DIR* dir=opendir(name);

	struct dirent* pd=NULL;
	while((pd=readdir(dir))!=NULL)
	{
		if(strcmp(".",pd->d_name)==0||strcmp("..",pd->d_name)==0)
		{
			continue;
		}
		cout<<pd->d_name<<"----------------------";
		//list(pd->d_name);
		switch(pd->d_type)
		{
			case DT_DIR:
			{
				char buf[255]="";
				cout<<"文件夹"<<endl;
				list(name);
				strcat(buf,name);
				strcat(buf,"/");
				strcat(buf,pd->d_name);
				//cout<<(pd->d_name)<<" ";
				show2(buf);
			}
			break;
			case DT_REG:
			{	
				cout<<"是普通文件"<<endl;
				list(pd->d_name);
				list(name);
			}
			break;
			default:
			{
				cout<<"其他文件"<<endl;
				list(pd->d_name);
				list(name);
			}
		}
	}
	closedir(dir);
}

int main(int argc,char* argv[])
{
	if(2==argc)//只有两个参数
	{
		char buf[]="/home/CHH/桌面/test/0907";
		show2(buf);
	}
}
//s.st_mode   文件夹 0040000==0040000   

猜你喜欢

转载自blog.csdn.net/Superman___007/article/details/82754231