Thread: C function to obtain a list of specific files in a directory

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    C function to obtain a list of specific files in a directory

    I am currently working on a project that involves a large number of files (>100000) all with similar file names. ie.(1foo.0001,...,1foo.1223... and 2foo.0001 and so forth). I need a way to be able to somehow obtain a list or an array containing all of these file names. The primitive file names will be passed into the program ./myprog -i 1foo 2foo, for example. Is there a function call in c or c++ that would perform a similar function to ls, but keep returning filenames until all of the files have been processed.

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no portable (or standard) method in C.

    However, most operating systems provide functionality to do this. Under several variants of unix, "man glob" will be a useful starting point.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What do you mean "returning filenames"? The shell does the filename expansion. So your program will receive all of the filenames in the second parameter passed to main().

    If you do ./myprog -i * and your main() definition looked like int main(int argc, char **argv) then argc would contain the count of all of the command-line arguments (plus the name of the command itself) and argv would contain a NULL-terminated list of strings representing those arguments.

    argv would look something like:
    argv[0] - "./myprog"
    argv[1] - "1foo.001"
    argv[2] - "1foo.1223"
    .
    .
    .
    argv[argc - 1] - "2foo.001"
    argv[argc] - NULL

    Is that what you're looking for?
    Code:
    itsme@itsme:~/C$ cat cline.c
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
      int i;
    
      printf("argc = %d\n\n", argc);
    
      for(i = 0;i < argc;++i)
        printf("argv[%d] - %s\n", i, argv[i]);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./cline foo bar blee
    argc = 4
    
    argv[0] - ./cline
    argv[1] - foo
    argv[2] - bar
    argv[3] - blee
    itsme@itsme:~/C$
    And likewise:
    Code:
    itsme@itsme:~/C$ ls file[0-9].txt
    file1.txt  file2.txt  file3.txt
    itsme@itsme:~/C$ ./cline file[0-9].txt
    argc = 4
    
    argv[0] - ./cline
    argv[1] - file1.txt
    argv[2] - file2.txt
    argv[3] - file3.txt
    itsme@itsme:~/C$
    Last edited by itsme86; 11-22-2005 at 08:37 AM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > If you do ./myprog -i *
    Bear in mind that whilst most shells have a large argument space, it isn't likely to be infinite.

    When I have huge numbers of files to process, I typically do something like this
    Code:
    find . -name "foo*" | xargs ./myprog -i
    cat filelist | xargs ./myprog -i
    find is a really useful tool for finding files based on all sorts of properties.

    xargs groups a long list of filenames together and invokes your program with that set of filenames. It keeps doing this until there are no more filenames.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Listing specific files in a directory
    By knirirr in forum C Programming
    Replies: 14
    Last Post: 01-29-2008, 05:42 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM