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!
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";
What about something simpler like that?
#ifdef WIN32
const std::string prefixPath = "..\\";
#else
const std::string prefixPath = "..\\..\\";
#endif
std::string path = prefixPath + "Test\\Input\\input.txt";
PS You may have a typo in the last line, in which you may miss another \ before input.txt.
As an alternative, if your C++ compiler supports this C++11 feature, you may want to use raw string literals, so you can have unescaped \, e.g.:
std::string path = prefixPath + R"(Test\Input\input.txt)";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With