How to see if a filename is a directory?

This is a discussion on How to see if a filename is a directory? within the C Programming forums, part of the General Programming Boards category; I want a (preferrably POSIX-compatible) way to tell if a file is a directory under Windows and Linux, other than ...

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046

    How to see if a filename is a directory?

    I want a (preferrably POSIX-compatible) way to tell if a file is a directory under Windows and Linux, other than access() and stat(). Nothing has turned up so far . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try to open it for reading. If it's not there, fopen should fail. Of course, on a crappy OS where you can't open files already in use, it'll fail too, so I suppose that won't tell you for sure in that case. :P


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    But stat() is POSIX, so what's your question?
    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
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, I want a fast function if I can get one. I thought stat() was rather slow.

    The only other idea I had was to try to chdir() to the filename . . . if it fails, it's not a directory.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    You "think" it's slow based on what?
    By actually trying it?

    You're messing with things on disk, which despite their impressive sounding speeds are pitifully slow compared to the speed between processor and memory.

    Or decide that performance is more important, and write a wrapper myIsDirectory() function around something more OS specific.
    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.

  6. #6
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    I don't know if this is gonna help but recently (in unix) I used read() on a file descriptor. Then if it returned an error, and errno was EISDIR (or something like that, then I knew it was a directory.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 01:54 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  5. How to retrieve absolute filename directory
    By Silvercord in forum Windows Programming
    Replies: 5
    Last Post: 02-08-2003, 09:40 AM

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