How to get the file separator symbol in standard C/C++ : / or \?

前端 未结 7 1393
攒了一身酷
攒了一身酷 2020-12-29 20:53

I would like to write a function :

inline char separator()
{
    /* SOMETHING */
}

that returns the file separator of the system in standar

相关标签:
7条回答
  • 2020-12-29 21:21

    The accepted answer does not work under Cygwin. Cygwin compiled programs running on Windows can use the Windows style '\' separator, but it does not define _WIN32 or the likes. A modified solution that works under Cygwin:

    inline char separator()
    {
    #if defined _WIN32 || defined __CYGWIN__
        return '\\';
    #else
        return '/';
    #endif
    }
    

    or

    const char kPathSeparator =
    #if defined _WIN32 || defined __CYGWIN__
        '\\';
    #else
        '/';
    #endif
    
    0 讨论(0)
提交回复
热议问题