Thread: know the only file name in a directory(Linux)

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

    know the only file name in a directory(Linux)

    There is a local diretory alled directory which contins only one file, I want to know the name of the file using c.. here is what I have got from Net:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<dirent.h>
    
    void getFile (char *);
    
    main()  {
            char s[50];
            getFile(s);
            printf("%s", s);
    }
    void getFile (char *file)
    {
        char dir[100] = "directory/";
        DIR *dp;
        struct dirent *dirp;
        if((dp  = opendir(dir)) == NULL) {
            perror("\nUnable to open directory.");
            exit(0);
        }
        dirp = readdir(dp);
        strcpy(file,dirp->d_name);
        closedir(dp);
    }
    There is file there in the directory though I get output as:

    Code:
    .
    Is there any other good way to do this? OR what is the problem with this code?
    Note: the folder contains only one file.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Actually readdir() will usually have at least 2 entries for an empty directory.

    The readdir() function shall not return directory entries containing empty names. If entries for dot or dot-dot exist, one entry shall be returned for dot and one entry shall be returned for dot-dot; otherwise, they shall not be returned.
    See the following link readdir. This link also contains an example.


    Jim
    Last edited by jimblumberg; 10-15-2010 at 07:27 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to call readdir() in a loop until it returns NULL.

    Each call gets you another filename.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    134
    Thanks all.. but there is no way to know the only file name present in the directory? and what are '.' and '..'?

    What I get from you that, I need to check for the names . and .. if the name is not . or .. then that is the name what I am looking for? am I thinking right?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    As Salem said
    You need to call readdir() in a loop until it returns NULL.

    Each call gets you another filename.
    The "." is the entry for the current directory. The ".." is the entry for the previous directory.

    Jim

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What leads you to believe there is only ONE filename in the directory?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM