Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a OS independent path in c++

I have a destination path and a file name as strings and I want to concatenate them with c++.

Is there a way to do this and let the program/compiler choose between / and \ for windows or unix systems?

like image 560
Janusz Avatar asked Jun 29 '09 02:06

Janusz


2 Answers

If you wanted to do it at compile time you could certainly do something like

#ifdef WIN32
#define OS_SEP '\\'
#else
#define OS_SEP '/'
#endif

Or you could just use '/' and things will work just fine on windows (except for older programs that parse the string and only work with '\'). It only looks funny if displayed to the user that way.

like image 151
jcopenha Avatar answered Oct 10 '22 23:10

jcopenha


As is so often the case, Boost has a library that does what you want. Here's a tutorial.

like image 40
David Seiler Avatar answered Oct 10 '22 21:10

David Seiler