2

I need a C/C++ function like os.path.isdir(path) in Python.

I found a very similar question but i'm using Linux.

Community
  • 1
  • 1
  • That [question you linked](https://stackoverflow.com/questions/146924/how-can-i-tell-if-a-given-path-is-a-directory-or-a-file-c-c) contains a [cross-platform answer](https://stackoverflow.com/a/43281413/4566599) which also works with C++ in Linux. – Roi Danton Jul 24 '18 at 12:50

1 Answers1

7

The POSIX solution is stat():

These functions return information about a file.

Basically, you hand it an instance of the struct stat, and if the call succeeds (check this first!) you get various fields filled in that describe the file.

You can then use the S_ISDIR() macro on the st_mode field to figure out if it's a directory. I suspect this is what Python does, under the hood.

unwind
  • 391,730
  • 64
  • 469
  • 606