know the only file name in a directory(Linux)

This is a discussion on know the only file name in a directory(Linux) within the C Programming forums, part of the General Programming Boards category; There is a local diretory alled directory which contins only one file, I want to know the name of the ...

  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
    2,690
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    2,690
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21