【发布时间】:2016-01-05 07:08:54
【问题描述】:
好的,所以我正在考虑一个显示我的文件夹或文件的最后修改时间的程序;我成功了,但我只传递了路径作为参数。
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/types.h>
int main(int count , char *args[]){
char buffer[50];
struct stat attr;
if(count < 2){
printf("No parameters passed");
}
else{
strcpy(buffer,args[1]);
stat(buffer,&attr);
printf("Last modiffied date time : % s" , ctime(&attr.st_mtime));
}
return 0;
}
哪个有效!但我想知道是否可以使用 DIR 而不是 char 路径来做到这一点。 比如:
.........
DIR *mydir;
struct dirent *dir;
dir = opendir(args[1]);
注意:但是我发现我之前使用的 stat 函数(路径作为参数)具有以下参数:
int stat(const char*restrict path, struct stat *restrict buf);
这让我想到了这个问题:
如果我不能使用此功能,我如何才能真正显示文件夹的统计信息(在我的情况下为上次修改日期)?
编辑
到目前为止,我使用 dirent 的尝试:
..........
DIR *mydir;
struct stat attr;
mydir = opendir(args[1]);
stat(mydir,&attr);
printf("last modified is... %s" , ctime(&attr.st_mtime));
return 0;
我收到此警告:
warning: passing argument 1 of ‘stat’ from incompatible pointer type /usr/include/sys/stat.h:211: note: expected ‘const char * __restrict__’ but argument is of type ‘struct DIR *’
【问题讨论】:
-
你可以统计一个目录
-
不太了解您的问题是什么。只需将
args[1]作为第一个参数传递给stat。或者,如果这不是您需要的,请澄清您的问题。 -
@AlanAu 谢谢我已经尝试过,但我收到警告,并且我的修改日期显示不正确;我已经编辑了我的问题。