Determine if Linux or Windows in C++

后端 未结 6 1880
忘了有多久
忘了有多久 2021-02-04 00:40

I am writing a cross-platform compatible function in C++ that creates directories based on input filenames. I need to know if the machine is Linux or windows and use the appropr

6条回答
  •  悲哀的现实
    2021-02-04 01:01

    Use:

    #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
    static const std::string slash="\\";
    #else
    static const std::string slash="/";
    #endif
    

    BTW, you can still safely use this slash "/" on Windows as windows understands this perfectly. So just sticking with "/" slash would solve problems for all OSes even like OpenVMS where path is foo:[bar.bee]test.ext can be represented as /foo/bar/bee/test.ext.

提交回复
热议问题