简体   繁体   中英

How to find file in directory in c

I'm trying to create a function in c which scans the given directory for files of the given format (for example:- _sort.txt) and check if the directory contains that format file or not.

if directory contain file of that format then it should return -1 else 0. but i am stuck on how to scan the directory.can anyone help me with this.

i am fairly new to c so please bear with me.

operating system:- Linux

you can use strstr to find the pattern in the filename.

don't forget to include these headers:
#include <dirent.h>
#include <string.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;                                                                                                                                                                      
}        

any where that you want to use this function you should pass the DIR which is opened with 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;                                                                                                                                                                          
}    

for further information about dirent struct :
man readdir

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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