I'm using C and sometimes I have to handle paths like
- C:\Whatever
- C:\Whatever\
- C:\Whatever\Somefile
Is there a way to check if a given path is a directory or a given path is a file?
I'm using C and sometimes I have to handle paths like
Is there a way to check if a given path is a directory or a given path is a file?
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_IFREG )
{
//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.)
This is a simple method using the GetFileAttributesW function to check if the path is a directory on Windows. If the received path must be a directory or a file path then if it is not a directory path you can assume that it is a file path.
bool IsDirectory(std::wstring path)
{
DWORD attrib = GetFileAttributes(path.c_str());
if ((attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)
return true;
return false;
}