【问题标题】:Passing a dir as argument and show its last modified date将 dir 作为参数传递并显示其最后修改日期
【发布时间】: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 谢谢我已经尝试过,但我收到警告,并且我的修改日期显示不正确;我已经编辑了我的问题。

标签: c date directory dir stat


【解决方案1】:

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

传递目录名有什么问题?

【讨论】:

  • 没有错,我只是想知道我是否可以使用另一种方法..这就是我想知道的。谢谢。
  • @Lazai 好吧,有fstat,它接受一个文件描述符,但我也不知道从DIR * 获取文件描述符的方法。
  • 是的,起初我想获取文件夹的 inode ......但不知道这对我有什么帮助。
猜你喜欢
  • 1970-01-01
  • 2013-11-23
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
  • 2021-05-05
  • 1970-01-01
相关资源
最近更新 更多