How can I get the platform-specific path separator (not directory separator) in C++, i.e. the separator that is needed to combine multiple paths in a list (e.g. PATH
environment variable).
Under Linux, this would be :
, under Windows ;
.
In other words, I am looking for the C++-equivalent of Python's os.pathsep
, Java's path.separator
or PHP's PATH_SEPARATOR
.
If Boost provides such a function that would be fine, as we are using it in our projects anyway. If not, I guess any other solution would be good.
All I could find (here and elsewhere) were either just ways to retrieve the directory separator (i.e. /
vs. \
) or related to other languages than C++.
If you prefer to hard-code the directory separator character, you should use the forward slash ( / ) character. It is the only recognized directory separator character on Unix systems, as the output from the example shows, and is the AltDirectorySeparatorChar on Windows.
pathSeparator would be ; . File. separator is either / or \ that is used to split up the path to a specific file. For example on Windows it is \ or C:\Documents\Test.
The path separator is a character commonly used by the operating system to separate individual paths in a list of paths.
path separator are platform dependent : For windows, it's '\' and for unix it's '/'.
As per comment, a possibility would be to use the preprocessor:
#ifdef _WIN32
const std::string os_pathsep(";");
#else
const std::string os_pathsep(":");
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With