【问题标题】:scan a directory to find files in c扫描目录以查找c中的文件
【发布时间】:2011-12-30 06:40:10
【问题描述】:

我正在尝试在 c 中创建一个函数,该函数扫描我的所有路径 C:\temp (Windows) 以搜索我通过的文件(例如 test.txt),每次找到一个返回步骤的路径另一个函数在这个文件的底部写一些东西。 我设法完成了写入文件的功能,但不知道如何扫描文件夹并传递找到的文件的地址。

【问题讨论】:

标签: c directory find


【解决方案1】:
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
void printdir(char *dir, int depth)
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;
    if((dp = opendir(dir)) == NULL) {
        fprintf(stderr,"cannot open directory: %s\n", dir);
        return;
    }
    chdir(dir);
    while((entry = readdir(dp)) != NULL) {
        lstat(entry->d_name,&statbuf);
        if(S_ISDIR(statbuf.st_mode)) {
            /* Found a directory, but ignore . and .. */
            if(strcmp(".",entry->d_name) == 0 ||
                strcmp("..",entry->d_name) == 0)
                continue;
            printf("%*s%s/\n",depth,"",entry->d_name);
            /* Recurse at a new indent level */
            printdir(entry->d_name,depth+4);
        }
        else printf("%*s%s\n",depth,"",entry->d_name);
    }
    chdir("..");
    closedir(dp);
}

int main()
{
    printf("Directory scan of /home:\n");
    printdir("/home",0);
    printf("done.\n");
    exit(0);
}

【讨论】:

  • ...但是我在哪里插入我的窗口扫描路径,如“c:\tools”,然后是我想要查找和修改的文件 txt,如“test.txt”,而不是我想要的字符串在它的末尾插入像“Sometext”?
  • 在函数printdir(“c:/xxx/xx/”,0)中传递目录名;根据您的需要修改代码,此代码将列出给定目录下的所有项目
  • 我已经像这样修改了在目录 LOG 中查找名为 Filter.txt 的文件并在文件末尾写“完成”...但不工作 int main(){ printf("Directory扫描 /home:\n"); printdir("C:/LOG/Filter.txt",0); printf("完成。\n");退出(0);}
  • int main() { printf(“/home 目录扫描:\n”); printdir("C:/LOG/Filter.txt",0); printf(“完成。\n”);退出(0); }
  • 请注意,虽然chdir(dir); 可以,但结尾chdir(".."); 并不是解决“将目录更改回原来的位置”问题的通用解决方案。我认为,它仅在命名目录是当前目录的直接子目录时才有效。在 POSIX 系统上,您可以使用 fchdir() 安全地更改目录:int cwd = open(".", O_RDONLY); 在您执行初始 chdir() 之前打开当前目录,然后 fchdir(cwd) 更改回该目录 - 当然,随后是 close(cwd)
【解决方案2】:

使用FindFirstFile 函数。 Here's a这个函数使用的好例子。

【讨论】:

    【解决方案3】:
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<sys/dir.h>
    #include<dirent.h>
    #include<fcntl.h>
    #include<unistd.h>
    #include<string.h>
    void searchInDirectory(char *dirname){
        DIR *dir;
        struct dirent *dirp;
        dir=opendir(dirname);
        chdir(dirname);
        while((dirp=readdir(dir))!=NULL){
            if(dirp->d_type==4){
                if(strcmp(dirp->d_name, ".")==0 || strcmp(dirp->d_name, "..")==0){
                    continue;
                }
                printf("%s %s\n", "FOLDER", dirp->d_name);
                searchInDirectory(dirp->d_name);
            }
            else{
                printf("%s %s\n", "FILE", dirp->d_name);
            }
        }
        chdir("..");
        closedir(dir);
    }
    int main(){
        searchInDirectory(".");
        return 0;
    }
    

    【讨论】:

    • 纯代码的帖子通常不受欢迎,因为它们没有向提问者解释你的答案与其他答案有何不同,或者你的代码提供了其他人没有提供的附加功能。回想一下,在回答问题时,您会扮演“老师”的角色。传递未注释的代码几乎没有教育意义(即使是正确的)。如果用户在使用opendir/readdir 时遇到问题,则不太可能通过简单地再次提供他们拥有的内容来解决他们的问题。你是一个新用户,所以这次不要这样做——但是在这里添加一个解释。
    • 我猜另一个答案也有同样的缺点——不要把它作为一个好的答案的例子。