Check if file is a directory

This is a discussion on Check if file is a directory within the C Programming forums, part of the General Programming Boards category; Hello. I'm having a small problem with a code i'm writing. I have to read all files in a directory, ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    4

    Check if file is a directory

    Hello.
    I'm having a small problem with a code i'm writing.
    I have to read all files in a directory, and, if i find a subdirectory, i have to enter and keep doing operations there.
    The problem is I don't know how to determine if the filename is a regular file or a directory.
    I am using a struct dirent structure, but i'm on a system which doesn't recognize d_type as a member of this structure.

    Any ideas?

    Thank you.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    First you need to use stat() on your struct dirent variable. Then you can do S_ISDIR(statbuf.st_mode) to see if it's a directory.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    4
    Thank you. I didn't know about that.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You could also try access().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM

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