How to check if a string sets a directory?

Suppose there is some line:

std::string some_string = "some_string"; 

And I want to know if chdir(some_string.c_str()) return -1 or not without calling it. Is there a quick way to do this?

PS I want my code to work for windows _chdir() , there I will use _chdir()

+4
source share
4 answers
 #ifdef WIN32 # include <io.h> #else # include <unistd.h> #endif int access(const char *pathname, int mode); // check user permissions for a file 

int mode values:

00 - Existence Only, 02 - For Write Only, 04 - For Read Only, 06 - Read and Write.

The function returns 0 if the file has the specified mode.

+2
source

I would use the Boost is_directory function , you can find more information on the Boost Fileystem Page .

+2
source

I want to know if chdir(some_string.c_str()) return -1 or not without calling it

You must be careful when conducting such checks. The problem is that if you rely on their result, because, while you are doing the validation and performing an operation that relies on the validation, another process could have performed the operation ( rmdir in this case), which invalidates the assumption value in your code, that is, you can enter the risk of a race in your code.

+2
source

Since you want it to work on Windows, use GetFileAttributes() , which returns File Attribute Constants .

GetFileAttributesEx even better.

0
source

Source: https://habr.com/ru/post/1347092/


All Articles