Programmer Programmer - 1 month ago 7
C++ Question

Convert from #define to string c++

I would like to convert from #define to string.

My code:

#ifdef WIN32
#define PREFIX_PATH = "..\\"
#else
#define PREFIX_PATH = "..\\..\\"
#endif


#define VAL(str) #str
#define TOSTRING(str) VAL(str)

string prefix = TOSTRING(PREFIX_PATH);
string path = prefix + "Test\\Input\input.txt";


But, it didn't work..

prefix value is "..\\\"

Any idea what is the problem..

Thanks!

Answer

You don't need "=" in defines, or any #str, or "+" between double quoted strings.

#ifdef WIN32
    #define PREFIX_PATH "..\\"
#else
    #define PREFIX_PATH "..\\..\\"
#endif

string path = PREFIX_PATH "Test\\Input\\input.txt";
Comments