Search txt file and put name in a ...

Discussion in 'C Programming' started by collinm, Mar 23, 2005.

  1. collinm

    collinm Guest

    i try to search txt file in a folder

    my code

    Code (Text):
    int searchfile(char *dir, char *filename, int flength, char *ext)
    {
        DIR *pdir;
        pdir = opendir(dir);
        struct dirent *pent;
        if (!pdir)
        {
            printf ("%s - Incapable d'utiliser opendir()\n", strerror
    (errno));
            return 1;
        }

        while((pent = readdir(pdir)) != NULL)
        {
            if(strcmp(pent->d_name + flength, ext) == 0)
            {
                printf("%s\n", pent->d_name);
            }
        }
        closedir(pdir);
        return 0;
    }
     
    i call this function with
    Code (Text):

    searchfile(local_dir_led,"", 28, ".txt");
     

    all my file name are the same size...
    they are surely a better way to do it (the file name size are not the
    same)...

    i would like to put the file name in a list....
    what is the better way to do it?
     
    collinm, Mar 23, 2005
    #1
    1. Advertisements

  2. collinm

    Default User Guest

    That doesn't sound good. Magic numbers are usually very poor practice.

    Sure.

    const char *extension = ".txt";
    size_t len;
    len = strlen (filename) - strlen (extension);

    That gives you the length of the base filename. I wouldn't even bother
    passing that into the function, just have searchfile compute it.

    What kind of list? Linked list? Expandable array? Without knowing what
    you intend to do with them it's hard to advise.



    Brian
     
    Default User, Mar 23, 2005
    #2
    1. Advertisements

  3. collinm

    CBFalconer Guest

     
    CBFalconer, Mar 23, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.