linux 下读取文件目录下所有文件大小以及修改日期写入txt
#include <stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>
#include<dirent.h>
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dt;//存儲文件命名
struct stat buf;/ /讀取文件修改時間
int ret;
struct tm *t;
//da kai mu lu
dp=opendir("/home/linux/io/day/day9");
if(dp==NULL)
{
perror("opendir");
return 0;
}
// bian li mu lu
while((dt=readdir(dp))!=NULL)
{
printf("%s\n",dt->d_name);
//du qu wen jian shu xin
if(*(dt->d_name)!='.'){
ret=stat(dt->d_name,&buf);
if(ret<0)
{
perror("stat");
return 0;
}
printf("size:%7d ",(int)buf.st_size);
t=localtime(&buf.st_ctime);
printf(" %d-%d-%d %d:%d\n",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min);
}
else{
puts("");
}
}
return 0;
}