How to know if a listed file item is a dir

This is a discussion on How to know if a listed file item is a dir within the C++ Programming forums, part of the General Programming Boards category; I need to list files and directories, including subfolders, filtering output, all common functions regarding filesystem. For all this I've ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    28

    Question How to know if a listed file item is a dir

    I need to list files and directories, including subfolders, filtering output, all common functions regarding filesystem. For all this I've chosen the "stat" function and "scandir" function but I seem to lack of a way to know if a listed item of scandir is a directory or not. Of course, "not having extension" is not a good way to solve this, since files do not necessarily do...

    Also it'd be great if someone would point me to a good specific source on this subject, with snippets covering most common functions. I'm not lazy to code it, but is highy improbable that I'll make a more efficient version than most out there...

    Thanks in advance,

    Mariano Lopez-Gappa (20) from Argentina

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    depends on operating system -- MS-Windows use FindFirstFile() and FindNextFile(), or *nix use opendir() and readdir(). Search these boards for those functions because there have been several examples posted. In MS-Windows os you can limit the files returned by FindFirstFile and FindNextFile by providing search criteria with wild cards, such as "*.txt". Don't know if you can do that with *nix. In *nix, use stat() to find out if the file is a directory. MS-Windows returns that information in the structure returned by FindFirstFile and FindNextFile, so stat() is unnecessary. If it is a directory then you can call the function recursively to process it.
    Last edited by Ancient Dragon; 11-27-2005 at 07:30 PM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    ya, thing is that I don't want to include windows.h since something tells me it won't work on my 486 where I'll be coding... scandir and stat do work

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    if its windows os what makes you think it won't work? FindFirstFile() and FindNextFile() work on all version of MS-Windows 95 and later. I never heard of scandir(). must be some compiler-specific function you are using.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    ya, scandir is linux-specific probably

    it's not windows, it's DOS, that's why it's probably not gonna work

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    what compiler are you using -- each compiler has its own way of searching for files. Turbo C uses findfirst() and findnext() which are declared in dir.h. MSVC 1.52C uses something like _dos_findfirst() in dos.h.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    sorry I didn't reply earlier, too many exams plus lazy memory. Ima probably use BCC32 from Borland, the free version, so I guess it's just looking into BCC32 documentation...thx for the insight Ancient Dragon you've been very helpful.

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. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM

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