I am trying to read in user input which is a file path (riddled with escape characters) into a CString. How can I do this without asking the user to replace '\' with '\\'?

For instance:

``````CString myString;
myString = _T("c:\test\file\j\mypath\path\user.bin");
wcout << myString.GetBuffer(myString.GetLength()) << endl;``````

outputs:

c: estQilejmypathpathuser .bin

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Jsplinter
Featured Replies
• 1

The only time \\ is needed is for string literals, like one you posted because the compiler has to interpret each of the characters in the string literal. When the path is typed in from the keyboard of read from a file the \ does not have to be escaped …

• 1

Ancient D. is correct. Try running this program, noticing the output when you type path and filename. [code] #include <iostream> #include <string> int main() { std::string mystring; std::cout << "Enter path and filename->"; std::cin >> mystring; for (int i = 0; i < mystring.size(); ++i) std::cout << mystring[i] << " …

The only time \\ is needed is for string literals, like one you posted because the compiler has to interpret each of the characters in the string literal. When the path is typed in from the keyboard of read from a file the \ does not have to be escaped because the compiler will never see it. So typing the path in an edit control then coping it to a CString object can be done without escape characters.

I see, thank you

Ancient D. is correct. Try running this program, noticing the output when you type path and filename.

``````#include <iostream>
#include <string>

int main()
{
std::string mystring;

std::cout << "Enter path and filename->";
std::cin >> mystring;

for (int i = 0; i < mystring.size(); ++i)
std::cout << mystring[i] << " ";

std::cout << std::endl;
return 0;
}``````