C: S_ISDIR 无法正常工作,只有 "."和 ".."显示为目录

标签 c string io

出于某种原因,当我遍历目录中的所有文件/文件夹并根据 S_ISDIR 检查我当前的选择时,它仅适用于“。”和“..”目录。即使我有 3 个文件(A、B、C)和 2 个文件夹(subdir、secondir)

代码:

//will read all files inside current directory, logic steps down into any sub directory found
int readDir(char * opt_basedir) // opt_basedir is the folder within the root repository of the .exe file, in this case "test"
{
struct dirent *dirr;

 DIR *directory; // used to keep track of current directory
struct stat fileStat; //used for lstat to hold stat info for the document
 directory = opendir("."); // open root
 directory = opendir(opt_basedir); // iterate straight into the selected folder


dirr = readdir(directory);
 while(dirr){

   lstat(dirr->d_name, &fileStat);

   if(S_ISDIR(fileStat.st_mode))
   {
      printf("only prints of . and .. =>" );
   }
    //if file (open)
    //readCopy(opt_basedir, &block, &offset);
    // else folder
    //function call
     printf("%s\n",dirr->d_name);
     dirr = readdir(directory);
 }


}

不确定我在这里做错了什么...... :(

最佳答案

您必须首先确保 opt_basedir 是您当前的工作目录。

你可以做类似的事情

chdir(opt_basedir);
directory = opendir(".");

代替

directory = opendir(opt_basedir);

另一种方法是创建绝对路径:

directory = opendir(opt_basedir);
/* error check */

while ((dirr = readdir(directory))) {
    char *path = malloc(strlen(opt_basedir) + strlen(dirr->d_name) + 1);
    /* error check */
    strcpy(path, opt_basedir);
    strcat(path, dirr->d_name);

    lstat(path, ...);
    /* other code */

    free(path);
}

这是一个完整的测试程序(注意:readDir()中的输出仅用于调试):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>

void
readDir(const char *path)
{
    DIR *dp;
    struct dirent *dr;
    struct stat fs;

    dp = opendir(path);
    if (dp == NULL) {
        perror("opendir");
        return;
    }

    printf("%s:\n", path);
    while ((dr = readdir(dp))) {
        if (!strcmp(dr->d_name, ".") || !strcmp(dr->d_name, "..")) {
            continue;
        }
        char *abs_path = malloc(strlen(path) + strlen(dr->d_name) + 2);
        if (!abs_path) {
            perror("malloc");
            exit(EXIT_FAILURE);
        }
        strcpy(abs_path, path);
        strcat(abs_path, "/");
        strcat(abs_path, dr->d_name);
        if (lstat(abs_path, &fs) < 0) {
            free(abs_path);
            continue;
        }
        if (S_ISDIR(fs.st_mode)) {
            readDir(abs_path);
        }
        printf("\t%s\n", dr->d_name);
        free(abs_path);
    }
}

int
main(int argc, char *argv[])
{
    if (argc != 2) {
        fprintf(stderr, "Usage: %s dir\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    readDir(argv[1]);

    exit(EXIT_SUCCESS);
}

关于C: S_ISDIR 无法正常工作,只有 "."和 ".."显示为目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22930709/

相关文章:

c - 在 C 中覆盖文件中的行,奇怪的输出

c++ - 带分隔符的getline

Java 解压缩带有文件夹 FileNotFoundexception 的压缩存档

c - 栈是如何展开的,栈帧是如何识别的

c - 从数组中读取数字然后将它们插入列表的程序不起作用

c - 从C程序中,如何知道进程是在前台还是后台运行?

javascript - 根据对象中的键值条目用值替换字符串中的键

python - 如何检查字符串是否为 rgb 十六进制字符串

c - 什么 C 库允许缩放巨大的图像?

python - 查找字符串中出现次数最多的字符