c - 传递目录作为参数并显示其最后修改日期

标签 c date directory dir stat

好的,所以我正在考虑一个程序来显示我的文件夹或文件的上次修改时间;我成功地做到了这一点,但我只传递了路径作为参数。

#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 *’

最佳答案

答案是否定的。您不能将 DIRstat 一起使用。

传递目录名称有什么问题?

关于c - 传递目录作为参数并显示其最后修改日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33002613/

相关文章:

c++ - LLVM 错误 : Broken function found, 编译中止!在 removeFromParent() 之后

c - 通过渲染四边形更新粒子系统的速度和位置

php - Zend 和 Doctrine 的日期格式

php - 链接到 css 文件 PHP 的多个文件夹

在 Mac OS X 上使用 SWIG 编译 Ruby 库

mysql - 葡萄牙语 MYSQL 中的 MONTHNAME

Javascript: `new Date(dateString)` 与 `new Date(year, month, day)` 之间的区别

文件名的Python问题

python - 什么时候应该使用 pathlib.Path.mkdir() 与 os.mkdir() 或 os.makedirs()?

flex 和 bison 的通用标记