Lets say I have this function:
void printPathToFile( std::wstring myPath ){
std::wstringstream ss;
ss << myPath;
//When printing myPath as soon as there is a \ it stops so this information is lost.
ss << getOtherReleventLogingInformation();
std::wofstream myfile;
myfile.open ("C:\\log.txt", std::wofstream::app|std::wofstream::out);
myfile << ss.str();
myfile.close();
}
I do not control the myPath
argument. Right now it doesn't have \\
in the path name so the stream interprets them as escape sequence which is not what I want.
How can I use a std::wstring variable as a raw string?
If it was a string literal I could use R"C:\myPath\"
but how do I achieve the same thing without a string literal?
A possible approach could be to loop through the path name and add an extra backslash where needed but surely c++ has something more robust and elegant..?
EDIT:
My problem was misdiagnosed. Turns out the backslashes don't cause any trouble what I had to add was:
#include <codecvt>
#include <locale>
const std::locale utf8_locale
= std::locale(std::locale(), new std::codecvt_utf8<wchar_t>());
myFile.imbue(utf8_locale);
As explained here: Windows Unicode C++ Stream Output Failure
The file path now displays correctly, I thought using a wofstream
took care of the locals for you so the non-ANSII characters would display correctly.