How to use S_ISREG() and S_ISDIR() POSIX Macros?

You’re using S_ISREG() and S_ISDIR() correctly, you’re just using them on the wrong thing. In your while((dit = readdir(dip)) != NULL) loop in main, you’re calling stat on currentPath over and over again without changing currentPath: if(stat(currentPath, &statbuf) == -1) { perror(“stat”); return errno; } Shouldn’t you be appending a slash and dit->d_name to currentPath … Read more

php readdir problem with japanese language file name

I can’t speak definitively for PHP, but I suspect it’s the same basic problem as with Python 2 had (before later adding special support for Unicode string filenames). My belief is that PHP is dealing with filenames using the standard C library ‘open’-et-al functions, which are byte-based. On Windows (NT) these try to encode the … Read more

Checking if a dir. entry returned by readdir is a directory, link or file. dent->d_type isn’t showing the type

d_type is a speed optimization to save on lstat(2) calls, when it’s supported. As the readdir(3) man page points out, not all filesystems return real info in the d_type field (typically because it would take an extra disk seek to read the inode, as is the case for XFS if you didn’t use mkfs.xfs -n … Read more

Does readdir() guarantee an order?

The readdir method doesn’t guarantee any ordering. If you want to ensure they are sorted alphabetically you’ll need to do so yourself. Note: I searched for a bit for definitive documentation saying this is the case. The closest I came is the following link http://utcc.utoronto.ca/~cks/space/blog/unix/ReaddirOrder It’s by no means definitive but it does give a … Read more

Microsoft Visual Studio: opendir() and readdir(), how?

I would suggest using FindFirstFile() and FindNextFile(). sample code: HANDLE hFind; WIN32_FIND_DATA FindFileData; if((hFind = FindFirstFile(“C:/some/folder/*.txt”, &FindFileData)) != INVALID_HANDLE_VALUE){ do{ printf(“%s\n”, FindFileData.cFileName); }while(FindNextFile(hFind, &FindFileData)); FindClose(hFind); } This really is better, because i can use “*.txt” etc, makes it much more easier to find some specific filetypes, earlier i had to write own match function for … Read more