C++ - Determining if directory (not a file) exists in Linux

MetaDark picture MetaDark · Feb 12, 2011 · Viewed 79.5k times · Source

How would I determine if a directory (not a file) existed using C++ in Linux? I tried using the stat() function but it returned positive when a file was found. I only want to find if the inputted string is a directory, not something else.

Answer

OneOfOne picture OneOfOne · Feb 12, 2011

According to man(2) stat you can use the S_ISDIR macro on the st_mode field:

bool isdir = S_ISDIR(st.st_mode);

Side note, I would recommend using Boost and/or Qt4 to make cross-platform support easier if your software can be viable on other OSs.