Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check OS version in a cross platform fashion with C++?

Well, I think the question is fairly objective. So, is there anyway to do it?

Actually, I need to check what version of the OS because I want to get a directory from a string:

"directory/file.ext"

or

"directory\file.ext"

So I split one of those strings according to "\" or to "/", depending on which OS I'm dealing with.

I'm quite new to C++ and I don't know if there's another way of getting a directory from a path string.

I can do that fairly well with C#, which will be my last alternative.

Best regards and thanks in advance!

like image 587
Girardi Avatar asked Jan 28 '26 05:01

Girardi


2 Answers

Actually, you don't need to do that. The POSIX style "/" will work as a directory separator just fine, even on a Win32 system (try it out.)

That said, other filesystem operations are platform-specific (drive letters, for instance.)

The Boost Filesystem library is a good option - code to this, and it will handle the platform details for you: http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/index.htm

Other cross-platform C++ frameworks, such as Qt, have similar facilities.

You can delve into more details here: http://en.wikipedia.org/wiki/Path_(computing)

like image 140
holtavolt Avatar answered Jan 30 '26 18:01

holtavolt


You can use #ifdef WIN32 to decide if you are on Windows. This works even on 64-bit Windows.

like image 27
Miguel Avatar answered Jan 30 '26 20:01

Miguel