gpt4 book ai didi

c - 在递归文件遍历 C 期间获取完整文件路径

转载 作者:行者123 更新时间:2023-12-01 17:35:48 30 4
gpt4 key购买 nike

我正在递归遍历目录以更改文件。我的更改文件功能需要文件的完整路径才能执行操作。但是,我的程序现在正在做的只是获取当前文件或文件夹的名称,而不是完整路径。

我的方法是创建一个字符串并不断向其附加名称,直到获得完整路径。但是,因为我正在执行递归,所以我在传递字符串以向其附加更多字符串时遇到了麻烦。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>

void recursiveWalk(const char *pathName, char *fullPath, int level) {
DIR *dir;
struct dirent *entry;

if (!(dir = opendir(pathName))) {
fprintf(stderr, "Could not open directory\n");
return;
}

if (!(entry = readdir(dir))) {
fprintf(stderr, "Could not read directory\n");
return;
}

do {
if (entry->d_type == DT_DIR) { // found subdirectory
char path[1024];

int len = snprintf(path, sizeof(path)-1, "%s/%s", pathName, entry->d_name); // get depth
path[len] = 0;

// skip hidden paths
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}

fprintf(stdout, "%*s[%s]\n", level*2, "", entry->d_name);

// Append fullPath to entry->d_name here

recursiveWalk(path, fullPath, level + 1);
}
else { // files
fprintf(stdout, "%*s- %s\n", level*2, "", entry->d_name);

//changeFile(fullPath);
}
} while (entry = readdir(dir));

closedir(dir);
}

int main(int argn, char *argv[]) {
int level = 0;
recursiveWalk(".", "", level);

return 0;
}

最佳答案

您的代码中存在一些小问题。

  • 永远不要在 recursiveWalk 中使用或更改 fullPath
  • 您的格式很奇怪:您使用level*2 来限制从空字符串打印的字符数
  • 只有在找到目录时才计算实际路径,而您说需要它来更改文件。
  • snprintf 后添加 path[len] = 0snprintf 保证但缓冲区为 null 终止时

但除此之外,您正确地将分析目录的路径附加到初始调用中传递的路径,但在 pathName 变量中,并计算为 path

因此,您的代码可能的修复方法是:

  • 修复 printf 的格式
  • recursiveWalk 中删除未使用的 fullPath 参数
  • 始终计算路径并在文件分支中使用它
  • 注释掉不必要的path[len] = '\0'
  • 我还将 while (entry = readdir(dir)); 替换为 while ((entry = readdir(dir))); 以明确地告诉编译器我想要设置条目然后测试其值 - 并删除警告

可能的代码:

#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>

void recursiveWalk(const char *pathName, int level) {
DIR *dir;
struct dirent *entry;

if (!(dir = opendir(pathName))) {
fprintf(stderr, "Could not open directory\n");
return;
}

if (!(entry = readdir(dir))) {
fprintf(stderr, "Could not read directory\n");
return;
}

do {
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", pathName, entry->d_name); // get depth
// path[len] = 0;
if (entry->d_type == DT_DIR) { // found subdirectory


// skip hidden paths
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}

fprintf(stdout, "%s [%s] (%d)\n", pathName, entry->d_name, level);

// Append fullPath to entry->d_name here

recursiveWalk(path, level + 1);
}
else { // files
fprintf(stdout, "%s (%d)\n", path, level);

//changeFile(fullPath);
}
} while ((entry = readdir(dir)));

closedir(dir);
}

int main(int argn, char *argv[]) {
int level = 0;
recursiveWalk(".", level);

return 0;
}

关于c - 在递归文件遍历 C 期间获取完整文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31768830/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com