【IOday3作业】

1.显示指定路径下所有文件的权限 硬链接数 时间 以及名字

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


void get_filepermission(mode_t mode,char *p)
{    
	char per[]="rwx";
  for(int i=0;i<9;i++)
  {
	  if((mode & (0400>>i))==0)
	  {
         p[i]='-';
		 continue;
	  }
	  p[i]=per[i%3];
  }

}

int main(int argc, const char *argv[])
{
	struct stat p;
	struct tm *time_01=NULL;
	char arr[10]="";

	DIR *dp=opendir("../day3/");
	if(NULL == dp)
	{
		perror("opendir");
		return -1;
	}
	struct dirent *rp=NULL;
	int count=1;

	while(1)
	{
		rp=readdir(dp);
		if(NULL==rp)
		{
			if(0==errno)
			{
				printf("读取完毕\n");
				break;
			}
			else
			{
				perror("readdir");
				return -1;
			}

		}
		if(rp->d_name[0]=='.')
			continue;
	if(stat(rp->d_name,&p)<0)
	{
		perror("stat");
		return -1;
	}
	get_filepermission(p.st_mode,arr);
	printf("%s\t",arr);
	printf("%ld\t",p.st_nlink);
    time_01=localtime(&p.st_ctime);
	printf("%d-%02d-%02d:%02d:%02d:%02d\t",time_01->tm_year+1900,time_01->tm_mon+1,time_01->tm_mday,time_01->tm_hour,time_01->tm_min,time_01->tm_sec);
    printf("%s\n",rp->d_name);
	}

	closedir(dp);
	return 0;
}

结果:

rwxrwxr-x	1	2023-05-04:14:04:19	2.jpg
rw-rw-r--	1	2023-05-04:18:58:59	zuoye.c
rwxrwxr-x	1	2023-05-04:10:50:53	open.txt
rw-rw-r--	1	2023-05-04:21:31:13	zuoye1.c
rw-rw-r--	1	2023-05-04:14:03:33	open_1.c
rwxrwxr-x	1	2023-05-04:21:31:17	a.out
rwxrwxr-x	1	2023-05-04:13:53:37	1.c
rw-rw-r--	1	2023-05-04:15:21:59	opendir_1.c
rw-rw-r--	1	2023-05-04:18:54:01	stat_01.c
读取完毕

你可能感兴趣的:(c++,c语言,算法)