繁体   English   中英

如何在c的目录中查找文件

[英]How to find file in directory in c

我正在尝试在 c 中创建一个 function,它会扫描给定目录中给定格式的文件(例如:- _sort.txt)并检查该目录是否包含该格式文件。

如果目录包含该格式的文件,那么它应该返回 -1 否则返回 0。但我对如何扫描目录感到困惑。任何人都可以帮助我解决这个问题。

我是 c 的新手,所以请多多包涵。

操作系统:- Linux

您可以使用strstr在文件名中查找模式。

不要忘记包含这些标头:
#include <dirent.h>
#include <字符串.h>

int find_file(const char *pattern, DIR *dr) {                                                                                                                                          
                                                                                                                                                                                       
    struct dirent *de;                                                                                                                                                                 
                                                                                                                                                                                       
    int found = -1;                                                                                                                                                                    
                                                                                                                                                                                       
    while ( (de = readdir(dr)) != NULL) {                                                                                                                                              
                     
        // DT_REG is a regular file                                                                                                                                                          
        if (de->d_type == DT_REG && strstr(de->d_name, pattern) != NULL) {                                                                                                             
                                                                                                                                                                                       
            found = 0;                                                                                                                                                                 
            break;                                                                                                                                                                     
        }                                                                                                                                                                              
    }                                                                                                                                                                                  
    return found;                                                                                                                                                                      
}        

任何你想使用这个 function 的地方你应该传递用opendir打开的DIR

int main() {                                                                                                                                                                           
                                                                                                                                                                                       
                                                                                                                                                                                       
    DIR *d = opendir(".");                                                                                                                                                             
    const char *pattern = "_sort.txt";                                                                                                                                                 
                                                                                                                                                                                       
    if (find_file(pattern, d) == 0)                                                                                                                                                    
        printf("found it\n");                                                                                                                                                          
    else                                                                                                                                                                               
        printf("not found it\n");                                                                                                                                                      
                                                                                                                                                                                       
                                                                                                                                                                                       
    return 0;                                                                                                                                                                          
}    

有关dirent struct的更多信息:
男人读目录

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM