How to tell if a file or directory

This is a discussion on How to tell if a file or directory within the C++ Programming forums, part of the General Programming Boards category; Hello, I need to determine if d_name in struct dirent is a file or directory. According to the Clibray I ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    56

    How to tell if a file or directory

    Hello,

    I need to determine if d_name in struct dirent is a file or directory. According to the Clibray I can use unsigned char d_type but it is not reliable for all posix systems. So, how would I determine if d_type is a file or directory without unsigned char d_type? Could I just check to see if it is a directory by opening a directory and if it opens, it is a directory and if it fails it is not a directory? Any ideas?

  2. #2
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Swappo,

    You can do a stat and then do an S_ISDIR on the st_mode

    Code:
    bool isDir(const char* target)
    {
       struct stat statbuf;
       stat(target, &statbuf);
       return S_ISDIR(statbuf.st_mode);
    }
    Don't forget your error checking.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    How many systems are you hoping to be portable with?

    Using d_type (IIRC, there are some macros as well such as ISDIR()), would be preferable.
    Edit: Zlatko beat me to the stat()

    The ultimate backstop would be to try opendir() and check the errno for say ENOTDIR

    In any event, creating your own wrapper function will isolate portability issues.
    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
    Nov 2008
    Posts
    56
    Thanks for the help. I found the page for struct stat. S_ISDIR and S_ISREG is what I am looking for.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by Salem View Post
    The ultimate backstop would be to try opendir() and check the errno for say ENOTDIR
    I wouldn't call that a backstop, I'd call it the proper solution. It's portable and it works.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 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

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