【问题标题】:ERROR: stat: No Such File Or Directory, opendir() and stat() in C programming错误:stat:C 编程中没有这样的文件或目录、opendir() 和 stat()
【发布时间】:2014-07-21 04:01:54
【问题描述】:

您好,感谢您的阅读。

我正在制作一个程序,它采用 1 个参数(目录)并读取使用 opendir()/readdir() 的目录中的所有文件,并使用 stat 显示文件类型(reg、链接、目录等)。当我在 shell 中执行我的程序时(我使用的是 redhat linux),我收到错误“没有这样的文件或目录”。这是我的代码:

#define _BSD_SOURCE
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

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

    DIR *dirp;
    struct dirent* dent;
    struct stat info;

//If no args
    if(argc == 1){

        argv[1] = ".";  

            dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
            do {
                dent = readdir(dirp);
                if (dent)
                {
//////////////////////////////////////////////////////////////////////////
               if (stat(dent->d_name, &info) == -1) {
               perror("stat");
               exit(EXIT_FAILURE);
           }

           switch (info.st_mode & S_IFMT) {
           case S_IFBLK:  printf("block device\n");            break;
           case S_IFCHR:  printf("character device\n");        break;
           case S_IFDIR:  printf("dir ");                      break;
           case S_IFIFO:  printf("FIFO/pipe\n");               break;
           case S_IFLNK:  printf("lnk ");                      break;
           case S_IFREG:  printf("reg ");                      break;
           case S_IFSOCK: printf("socket\n");                  break;
           default:       printf("unknown?\n");                break;
           }

//////////////////////////////////////////////////////////////////////////

                    printf("%s \n", dent->d_name);

                }
            } while (dent);
            closedir(dirp);

       }

////////////////////////////////////////////////////////////////////////////////////////////////
//If specified directory    
    if(argc > 1){

        dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
        do {
            dent = readdir(dirp);
            if (dent)
            {

/////////////////////////////////////////////////////////////////////////////////////           
               if (stat(dent->d_name, &info) == -1) {
               perror("stat");
               exit(EXIT_FAILURE);
           }



           switch (info.st_mode & S_IFMT) {
           case S_IFBLK:  printf("block device\n");            break;
           case S_IFCHR:  printf("character device\n");        break;
           case S_IFDIR:  printf("dir ");                      break;
           case S_IFIFO:  printf("FIFO/pipe\n");               break;
           case S_IFLNK:  printf("lnk ");                      break;
           case S_IFREG:  printf("reg ");                      break;
           case S_IFSOCK: printf("socket\n");                  break;
           default:       printf("unknown?\n");                break;
           }
//////////////////////////////////////////////////////////////////////////////////////
//           printf("%s\n", argv[1]);

                printf("%s \n", dent->d_name);
            }
        } while (dent);
        closedir(dirp);

   } 
return 0;    
}

有什么想法吗?我有点卡住了。 感谢您的意见

另外,“链接”类型的文件是否将使用 stat 输出,还是必须使用 lstat?不确定在这种情况下如何使用 lstat,如果我将结构类型更改为“struct lstat info”,则会引发错误。

【问题讨论】:

  • 你能展示你如何调用你的程序吗?
  • 要么像 "./a.out " 要么只是 "./a.out" 没有参数来做当前目录

标签: c linux directory stat


【解决方案1】:

dent-&gt;d_name 是相对于当前目录的文件名(例如“/home/barney/myfile.txt”)而不是文件的绝对完整路径(例如 /home/barney/sources/myfile.txt ),这是 stat 所期望的。

这就是 stat 找不到路径的原因。在每次调用 stat 之前打印dent-&gt;d_name 以观察那些不正确的路径。

编辑: 您可以尝试 chdir() 将您的工作目录更改为 argv[1]

【讨论】:

  • 哦,我明白了,这很有道理,你建议我如何纠正它?
  • 我在 if 语句中将 dent->d_name 更改为 argv[1],现在我没有收到此错误,但不确定输出是否正确,呵呵
  • 我在 if 语句之前使用了 dent->d_name 尝试了 chdir(argv[1]),但它给了我错误。我对 Stat 和 openDir 有点陌生,我做错了什么?
  • 我其实很傻,我忘记了#include,这就是为什么我在 chdir(); 上得到隐式声明错误的原因。 ....哈哈哈哇 facepalm
猜你喜欢
  • 2021-03-05
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多