Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recognize file type in C++

Tags:

c++

I implemented a solution to the problem meant to be cross-platform and adherent to the C++ standard library.

bool isdir(const char *string) {
   ofstream file(string, ios::out);
   return file.fail();
}

Yet, if the file is actually writable, the program opens an empty file with string name in its working directory. How can I prevent this from happening?

like image 329
Stencil Avatar asked Mar 04 '26 15:03

Stencil


1 Answers

There is no standard way of detecting if a file is a directory in C++. But you can use Boost.Filesystem. It is well portable.

Edit: It seems that this question has already been answered here.

like image 71
Juraj Blaho Avatar answered Mar 07 '26 07:03

Juraj Blaho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!