<p>How do i check in C++ if a file is a regular file (and is not a directory, a pipe, etc.)? I need a function isFile(). </p>

<pre><code>DIR *dp;
struct dirent *dirp;

while ((dirp = readdir(dp)) != NULL) {
if ( isFile(dirp)) {
     cout &lt;&lt; "IS A FILE!" &lt;&lt; endl;
i++;
}
</code></pre>

<p>I've tried comparing dirp->d_type with (unsigned char)0x8, but it seems not portable through differents systems. </p>

You need to call stat(2) on the file, and then use the S_ISREG macro on st_mode.
Something like (adapted from
this answer
):
```
```
#include <sys/stat.h>

struct stat sb;

if (stat(pathname, &sb) == 0 && S_ISREG(sb.st_mode))
{
    // file exists and it's a regular file
}
```
```