Vincent Vincent - 24 days ago 11
C Question

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

I would like to write a function :

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


that returns the file separator of the system in standard C/C++/C++11 ? (I mean slash or backslash depending on the system). Is there a way to achieve this ?

Answer

I'm not sure how to do it other than by checking ifdefs

inline char separator()
{
#ifdef _WIN32
    return '\\';
#else
    return '/';
#endif
}

or (as suggested by PaperBirdMaster)

const char kPathSeparator =
#ifdef _WIN32
                            '\\';
#else
                            '/';
#endif
Comments