Thread: Check if certain files exist, if so, open them

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    23

    Check if certain files exist, if so, open them

    Hi,
    I want to check if there are files present in a directory that match a certain regex, i.e., "reader_contact_info_%d", and if there are, I'd like to get the names of these files, perhaps the number that exist, and then open them and read them.

    I could do something via executing an ls command
    Code:
    ls reader_contact_info_*
    but there are performance implications as the current process will suspend while the ls command executes.

    I'm not sure how to do this though? It doesn't have to run on Windows, so that's not a problem.

    Thanks Much!
    Gz

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    By the way, "reader_contact_info_%d" is not a regex, and the "readercontact_info_*" is normally called a glob pattern. It is a simple form of regular expression (but is not a "regex" in the sense of this word).

    For handling files personally I like to use APR as it is portable and already includes some functions for handling files and patterns. What have you tried to solve your problem other than ls?

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This little directory-reading snippet might be of use. (I didn't test it, though.)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <dirent.h>
    
    int main() {
        struct dirent *ent;
        DIR *dir = opendir("directoryname");
        if (dir == NULL) {
            perror("Can't open directory");
            exit(EXIT_FAILURE);
        }
        while ((ent = readdir(dir)) != NULL) {
            printf("%s\n", ent->d_name);
        }
        closedir(dir);
        return 0;
    }
    @c99, reader_contact_info_%d could be a regex where %d means a single digit.

    EDIT:
    I decided to test it (seems to work) and add a little code to test for your "regex" without resorting to an actual regex.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <dirent.h>
    
    int main() {
        struct dirent *ent;
        char *str = "reader_contact_info_";
        size_t len = strlen(str);
        DIR *dir = opendir(".");  // current directory
    
        if (dir == NULL) {
            perror("Can't open directory");
            exit(EXIT_FAILURE);
        }
    
        while ((ent = readdir(dir)) != NULL) {
            if (strncmp(ent->d_name, str, len) == 0)
                if (isdigit(ent->d_name[len]))
                    printf("%s\n", ent->d_name);
        }
    
        closedir(dir);
        return 0;
    }
    Last edited by oogabooga; 01-24-2014 at 06:44 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    For using APR you can use the function apr_match_glob to read in a list of matching filenames into an APR array

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <apr.h>
    #include <apr_fnmatch.h>
    
    static void apr_fatal(apr_status_t rv, const char *msg, int exitcode)
    {
        static char buf[1001];
        printf("%s: %s (code %d)\n", msg, apr_strerror(rv, buf, 1000), rv);
        exit(exitcode);
    }
    
    static void fatal(const char *msg, int exitcode)
    {
        perror(msg);
        exit(exitcode);
    }
    
    void demo_glob(const char *dir, const char *pattern, apr_pool_t *p) {
        // Change directory to dir
        if (chdir(dir) != 0) {
            fatal("failed to chdir", 1);
        }
    
        // Fill result array
        apr_array_header_t *result = apr_array_make(p, 0, sizeof(const char *));
        apr_status_t rv;
        if ((rv = apr_match_glob(pattern, &result, p)) != APR_SUCCESS) {
            apr_fatal(rv, "failed to glob", 1);
        }
    
        // Print result array
        for (int i = 0; i < result->nelts; i++) {
            printf("%s\n", APR_ARRAY_IDX(result, i, const char *));
        }
    }
    
    int main(void)
    {
        apr_pool_t *p;
        apr_initialize();
        if (apr_pool_create(&p, NULL) != APR_SUCCESS) {
            exit(1);
        }
    
        demo_glob("c:/foo/bar", "*.baz", p);
    
        apr_pool_destroy(p);
        apr_terminate();
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    23
    That works. Thanks.


    Quote Originally Posted by oogabooga View Post
    This little directory-reading snippet might be of use. (I didn't test it, though.)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <dirent.h>
    
    int main() {
        struct dirent *ent;
        DIR *dir = opendir("directoryname");
        if (dir == NULL) {
            perror("Can't open directory");
            exit(EXIT_FAILURE);
        }
        while ((ent = readdir(dir)) != NULL) {
            printf("%s\n", ent->d_name);
        }
        closedir(dir);
        return 0;
    }
    @c99, reader_contact_info_%d could be a regex where %d means a single digit.

    EDIT:
    I decided to test it (seems to work) and add a little code to test for your "regex" without resorting to an actual regex.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <dirent.h>
    
    int main() {
        struct dirent *ent;
        char *str = "reader_contact_info_";
        size_t len = strlen(str);
        DIR *dir = opendir(".");  // current directory
    
        if (dir == NULL) {
            perror("Can't open directory");
            exit(EXIT_FAILURE);
        }
    
        while ((ent = readdir(dir)) != NULL) {
            if (strncmp(ent->d_name, str, len) == 0)
                if (isdigit(ent->d_name[len]))
                    printf("%s\n", ent->d_name);
        }
    
        closedir(dir);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2013, 01:33 AM
  2. Does ofstream::open create the file if it doesn't already exist?
    By Programmer_P in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2010, 04:52 PM
  3. Check if a directory exist?
    By kevinawad in forum C++ Programming
    Replies: 12
    Last Post: 10-26-2008, 02:42 AM
  4. hwo to check if a file exist in certain dir?
    By Jasonymk in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2003, 08:20 PM
  5. How to check a directory is exist or not?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2002, 10:13 AM