Johannes S.
Johannes S.

Reputation: 4626

How to get platform-specific path separator in 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++.

Upvotes: 9

Views: 6985

Answers (2)

hmjd
hmjd

Reputation: 121971

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

Upvotes: 6

Nick
Nick

Reputation: 25799

The only portable way beyond the use of the preprocessor that I can find would be through the Apache Portable Runtime:

apr_filepath_list_merge and apr_filepath_list_split

You could have a look and see how these are implemented but I'm guessing it would just be a preprocessor definition.

Upvotes: 4

Related Questions