File or Folder?

Hello forum!

I'm looking to determine if a filename is either a folder or a file.

I can list the files using dirent header but because it will not allow me to use d_type so I'm using stat instead.

As you can see I tried using two different methods from the documentation.

The second one outputs -1 and 0. However all files and folders are -1 except for one file that was 0.

Am I doing something wrong?

Thank you in advanced!

Charlotte

1
2
3
4
5
6

while (drct=readdir(dr)){
//1.    cout << S_ISDIR(drct->d_name) << endl;
//2.    cout << stat(drct->d_name, &_buf) << "   " << drct->d_name << endl;
}
Last edited on
Unix: // http://www.freebsd.org/cgi/man.cgi?query=stat&apropos=0&sektion=2&manpath=FreeBSD+9.0-RELEASE&arch=default&format=html

1
2
3
4
5
6
7
8
#include <sys/types.h>
#include <sys/stat.h>

bool is_directory( const char* path )
{
    struct stat sb ;
    return ( stat( path, &sb ) == 0 ) && S_ISDIR( sb.st_mode ) ;
}
Topic archived. No new replies allowed.