How to find file in directory in c

Viewed 705

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

1 Answers

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

Related