#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
int main(int argc, const char *argv[])
{
DIR* op=opendir("../文件IO/");//打开目录
if(NULL==op){ //判断目录是否打开成功
perror("opendir");
return -1;
}
struct dirent* pt= NULL; //
FILE *p=fopen("./file.txt","w");
struct tm* info;
while(1){
pt=readdir(op);
if(NULL==pt){
if(0==errno){
printf("目录读取完毕\n");
}
else{
perror("readdir");
return -1;
}
break;
}
struct stat buf;
if(stat(pt->d_name,&buf)<0){
perror("stat");
return -1;
}
mode_t m=buf.st_mode&0777;
info=localtime(&buf.st_atime);
fprintf(p,"0%o %d-%02d-%02d %02d:%02d:%02d %s\n",\
m,info->tm_year+1900,info->tm_mon+1,info->tm_mday,\
info->tm_hour,info->tm_min,info->tm_sec,pt->d_name);
}
closedir(op);
fclose(p);
return 0;
}
IO函数进程
最新推荐文章于 2024-09-12 23:51:52 发布