Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a given path is a directory or a file? (C/C++)

Tags:

c++

c

winapi

People also ask

How do you check if a path is a file or directory C?

How to test whether a given path is a directory or a file in C++? The type of a file/dir can be found out using lstat() and the macros on Linux. int lstat(const char *pathname, struct stat *statbuf); lstat return information about a file, in the buffer pointed to by statbuf.

How do you know whether it is a file or directory?

File. isDirectory() checks whether a file with the specified abstract path name is a directory or not. This method returns true if the file specified by the abstract path name is a directory and false otherwise.

How do you check if a file is a file in C?

access() Function to Check if a File Exists in C Another way to check if the file exists is to use the access() function. The unistd. h header file has a function access to check if the file exists or not. We can use R_OK for reading permission, W_OK for write permission and X_OK to execute permission.

Is directory a path?

A directory is a "folder", a place where you can put files or other directories (and special files, devices, symlinks...). It is a container for filesystem objects. A path is a string that specify how to reach a filesystem object (and this object can be a file, a directory, a special 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
}

Call GetFileAttributes, and check for the FILE_ATTRIBUTE_DIRECTORY attribute.


With C++14/C++17 you can use the platform independent is_directory() and is_regular_file() from the filesystem library.

#include <filesystem> // C++17
#include <iostream>
namespace fs = std::filesystem;

int main()
{
    const std::string pathString = "/my/path";
    const fs::path path(pathString); // Constructing the path from a string is possible.
    std::error_code ec; // For using the non-throwing overloads of functions below.
    if (fs::is_directory(path, ec))
    { 
        // Process a directory.
    }
    if (ec) // Optional handling of possible errors.
    {
        std::cerr << "Error in is_directory: " << ec.message();
    }
    if (fs::is_regular_file(path, ec))
    {
        // Process a regular file.
    }
    if (ec) // Optional handling of possible errors. Usage of the same ec object works since fs functions are calling ec.clear() if no errors occur.
    {
        std::cerr << "Error in is_regular_file: " << ec.message();
    }
}

In C++14 use std::experimental::filesystem.

#include <experimental/filesystem> // C++14
namespace fs = std::experimental::filesystem;

Additional implemented checks are listed in section "File types".


In Win32, I usually use PathIsDirectory and its sister functions. This works in Windows 98, which GetFileAttributes does not (according to the MSDN documentation.)