好的,所以我正在考虑一个程序来显示我的文件夹或文件的上次修改时间;我成功地做到了这一点,但我只传递了路径作为参数。
#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 *’
最佳答案
答案是否定的。您不能将 DIR
与 stat
一起使用。
传递目录名称有什么问题?
关于c - 传递目录作为参数并显示其最后修改日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33002613/