【问题标题】:How to find file in directory in c如何在c中找到目录中的文件
【发布时间】:2021-01-23 05:14:32
【问题描述】:

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

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

我对 c 还很陌生,所以请多多包涵。

操作系统:- Linux

【问题讨论】:

标签: c file directory


【解决方案1】:

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

不要忘记包含这些标题:
#include
#include

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;                                                                                                                                                                      
}        

任何你想使用这个功能的地方你都应该传递DIR,它是用opendir打开的。

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的更多信息:
ma​​n readdir

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    相关资源
    最近更新 更多