Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get platform-specific path separator in C++?

Tags:

c++

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++.

like image 609
Johannes S. Avatar asked May 09 '12 10:05

Johannes S.


People also ask

Which is the correct constant for a platform independent directory separator?

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.

Which character is used as a path separator?

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.

What is path separator?

The path separator is a character commonly used by the operating system to separate individual paths in a list of paths.

Which of the following is a file name path separator in Linux?

path separator are platform dependent : For windows, it's '\' and for unix it's '/'.


1 Answers

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
like image 78
hmjd Avatar answered Sep 24 '22 03:09

hmjd