【问题标题】:Viewing attributes of a file with stat (C)使用 stat (C) 查看文件的属性
【发布时间】:2016-03-14 23:21:22
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>


int main(int argc, char** argv) {

    struct stat *buf;
    buf = malloc(sizeof(struct stat)); 

    DIR * current_directory_ptr; /* DIR is a type from dirent.h */
    struct dirent * next_entry_ptr; /* struct dirent is a type from dirent.h */

    char* dirToView [200];

    printf("Enter path of desired directory\n");
    scanf("%s)", &dirToView);

    current_directory_ptr = opendir(dirToView);

    next_entry_ptr = readdir (current_directory_ptr);

    while(next_entry_ptr != NULL){
        printf("File has inode number %d and is called %s \n", 
             (int) next_entry_ptr ->d_ino, next_entry_ptr->d_name);
        next_entry_ptr=readdir(current_directory_ptr);

    }

    char* fileToView [200];
    printf("Enter name of desired file\n");
    scanf("%s)", &fileToView);



    stat(fileToView, buf);
    off_t size = buf -> st_size;
    printf("Size = %ld \n", size);

    uid_t owner = buf ->st_uid;
    printf("owner = %d \n", owner);


    closedir(current_directory_ptr);


    return (EXIT_SUCCESS);
}

此代码的目的是使用扫描来获取和输出所选文件的详细信息。打开一个目录是可行的,但是在打开一个文件时,大小和所有者的结果都是 0,而不管该文件的实际值如何。我相信这样做的原因是我将它们打印为错误的类型,但我不确定这一点。输出 stat() 结果的正确系统是什么?

为清晰起见进行编辑

问题在于下面突出显示的行

   char* fileToView [200];
   printf("Enter name of desired file\n");
   scanf("%s)", &fileToView);

   stat(fileToView, buf);
   off_t size = buf -> st_size;
   printf("Size = %ld \n", size);

   uid_t owner = buf ->st_uid;
  printf("owner = %d \n", owner);

程序中较早的代码按预期运行,至少有一个响应引用了较早的行。对于任何缺乏明确性,我深表歉意。

【问题讨论】:

  • 不要使用scanf("%s)", &amp;dirToView); 和scanf("%s)", &amp;fileToView);,它们会导致类型不匹配和未定义的行为。
  • 在这种情况下正确的类型是什么?有目录类型吗?
  • 使用char dirToView [200]; scanf("%199s)", dirToView);和char fileToView [200]; scanf("%s)", fileToView);

标签: c directory stat


【解决方案1】:

打印未知大小整数的正确方法是使用%jd/%ju 和intmax_t/uintmax_t 的中间转换:

printf("File has inode number %jd and is called %s\n", 
    (intmax_t) next_entry_ptr ->d_ino, next_entry_ptr->d_name);

intmax_t/uintmax_t 被定义为最大的整数有符号/无符号类型,可以表示任何其他整数类型的值。

强制转换本质上将整数扩大到最大大小,然后允许您以可移植的方式匹配格式说明符和类型。

【讨论】:

    【解决方案2】:

    您确实应该使用所有警告和调试信息进行编译(gcc -Wall -Wextra -g,如果使用GCC)并阅读诸如scanf(3) 和stat(2) 之类的函数的文档。请注意,两者都可能失败,您应该对其进行测试。

    你可以试试,而不是

    /// BAD CODE
    char* fileToView [200];
    printf("Enter name of desired file\n");
    scanf("%s)", &fileToView);
    stat(fileToView, buf);
    off_t size = buf -> st_size;
    printf("Size = %ld \n", size);
    

    更好的东西:

    char fileToView[200];
    memset (fileToView, 0, sizeof(fileToView));
    printf("Enter name of desired file:\n");
    if (scanf("%199s", fileToView)<=0) {
       perror("scanf"); exit(EXIT_FAILURE); }
    struct stat mystat;
    memset (&mystat, 0, sizeof(mystat));
    if (stat(fileToView, &mystat)) {
      fprintf(stderr, "stat '%s' failed: %s\n", fileToView, strerror(errno));
      exit(EXIT_FAILURE);
    }
    printf("for file '%s' size = %ld\n", fileToView, (long) mystat.st_size);
    

    请注意,我正在测试scanf 的成功;我将名称输入限制为 199 个(%s 不会接受任何空格,因为它会停止扫描它们);我将mystat 分配为局部变量(无需堆分配它),并且我正在测试stat 的失败;您的 fileToView 被错误地声明为一个包含 200 个指向 char 的指针的数组。我也有将所有局部变量归零的习惯。

    顺便说一句,您可以尝试strace(1) 您的程序。你会明白system calls在做什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多