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

Recommended Answers

All 3 Replies

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.

commented: I see, thank you +1

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;
}
commented: thank you for the example! +1

Thank you both. That cleared it up!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.