readdir returns info about all item within the directory. How can I tell if the item is file or directory?
Thank you.
EDIT. Sorry, forgot to mention, that my time target platform is iOS and android.
readdir returns info about all item within the directory. How can I tell if the item is file or directory?
Thank you.
EDIT. Sorry, forgot to mention, that my time target platform is iOS and android.
Check for d_type being D_DIR.
d_type is likely to be about 50-1000 times faster than calling stat for every entry, but you should probably include fallback code for systems where d_type does not exist or in case it's not filled in.It depends, on the standard "POSIX" readdir operation you do not have the field d_type
According to POSIX, the dirent structure contains a field char d_name[] of unspecified size, > with at most NAME_MAX characters preceding the terminating null byte. POSIX.1-2001 also documents the field ino_t d_ino as an XSI extension. Use of other > fields will harm the portability of your programs.
The "standard way" is to do a lstat call on you file/dir and check the st_mode field of the struct stat, if your st_mode match S_ISDIR, you have a dir.
You can also compute a path for each entry in the directory (but perhaps you can skip . & .. entries) and use the stat system call on that path.
This is useful if you want more information (e.g. modification time, size, ...) on the path.
d_type, though since the question is tagged linux, it is very unlikely to be a problem.
readdiris usable within an iOS application. In any case, on that platform you'll be better served by using NSFileManager and the-fileTypehelper method, which can identify directories.readdiris usable on iOS, but there are some bugs with its implementation on iOS 4.x: devforums.apple.com/message/251591 . I'd still recommend using the higher-level abstraction of NSFileManager for this.