I'm using C and sometimes i have to handle paths like C:\Whatever, C:\Whatever\ or C:\Whatever\Somefile
Is there a way to check if a given path is a directory or a given path is a file? :O
I'm using C and sometimes i have to handle paths like C:\Whatever, C:\Whatever\ or C:\Whatever\Somefile
Is there a way to check if a given path is a directory or a given path is a file? :O
Call GetFileAttributes, and check for the FILE_ATTRIBUTE_DIRECTORY attribute.
stat() will tell you this.
struct stat s;
if( stat(path,&s) == 0 )
{
if( s.st_mode & S_IFDIR )
{
//it's a directory
}
else if( s.st_mode & S_ISREG )
{
//it's a file
}
else
{
//something else
}
}
else
{
//error
}
In Win32, I usually use PathIsDirectory and its sister functions. This works in Windows 98, which GetFileAttributes does not (according to the MSDN documentation.)