Thread: find all files in current directory!

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    find all files in current directory!

    This is my previous post which returns all the filename in a folder,
    know the only file name in a directory(Linux)

    I want to list out all the file name in the current directory.
    I would like to know how to get the current directory, I would like to get simple and easiest way!

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    134
    have done this:

    Code:
    void getFile (char *file)
    {
        DIR *dir;
        struct dirent *dp;
    
            char cwd[1024];
    
           getcwd(cwd, sizeof(cwd));
    
        if((dir  = opendir(cwd)) == NULL) {
            perror("\nUnable to open directory.");
            exit(0);
        }
            while ((dp=readdir(dir)) != NULL) {
                    printf("%s\n", dp->d_name);
            }
            closedir(dir);
    
    }
    but why this is printing a garbage thing at the end?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You can open the current directory by doing opendir(".").

    What garbage thing is it printing? I tested it and it works fine on my system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Listing specific files in a directory
    By knirirr in forum C Programming
    Replies: 14
    Last Post: 01-29-2008, 05:42 AM
  2. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 08:39 AM
  3. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  5. Finding out how many files are in a directory
    By mikie7boy in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2003, 05:08 AM