How to insert "\\" in vc6 in c:\abc\def\aaa.bmp

titanium0203
titanium0203 used Ask the Experts™
on
Hi,

How can I insert "\\" in my absolute path?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Todd GerbertIT Consultant
Top Expert 2010

Commented:
Do you mean how to insert back-slashes in a string literal? Just put two consecutive back slashes each place in the string you want one.

char path[] = "C:\\abc\\def\\aaa.bmp";

Or...

char path[] = "\\\\server\\sharedFolder\\aaa.bmp";

Author

Commented:
Hi,

currently my apps return with a single back slash, is there any function
that can automatically conver it to double back slash.

thanks
ie your apps you mean that return value is string, than don't need nothing.
should work perfectly
char path[] = "C:\\abc\\def\\aaa.bmp";
is needed just wen you write in the code to convert it to string, but once converted that become
C:\abc\def\aaa.bmp
and no changes needed

but  you can do that if you want be replacing
string x = "C:\abc\def\aaa.bmp";
string _new = x.Replace("\\","\\\\");
IT Consultant
Top Expert 2010
Commented:
I'm not sure I understand what you're asking.

The code below produces the following output:
String with single back-slashes: C:\abc\def\aaa.bmp
String with a double back-slash: \\serverName\sharedFolder\aaa.bmp
Current working directory: C:\Users\Todd\Desktop\backslashes\backslashes
CWD with back-slashes replaced: C:\\Users\\Todd\\Desktop\\backslashes\\backslashes

Press ENTER to exit.

Open in new window


#include <iostream>
#include <string>
#include <direct.h>

#define MAX_PATH 260

using namespace std;

string& replaceChars(string& source, const string find, const string replacement)
{
	string::size_type pos = 0;
	while ((pos = source.find(find, pos)) != string::npos)
	{
		source.replace(pos, find.size(), replacement);
		pos += replacement.size();
	}
	return source;
}

int main(int argc, char **argv)
{
	char cwd[MAX_PATH + 1];
	string singleBackSlashes = "C:\\abc\\def\\aaa.bmp";
	string doubleBackSlashes = "\\\\serverName\\sharedFolder\\aaa.bmp";

	cout << "String with single back-slashes: " << singleBackSlashes << endl;
	cout << "String with a double back-slash: " << doubleBackSlashes << endl;
	
	_getcwd(cwd, MAX_PATH + 1);
	cout << "Current working directory: " << cwd << endl;

	string scwd(cwd);
	replaceChars(scwd, "\\", "\\\\");
	cout << "CWD with back-slashes replaced: " << scwd << endl;

	cout << endl;
	cout << "Press ENTER to exit.";

	cin.get();
}

Open in new window

Janusz CzopowikSenior Software Engineer

Commented:
All the reason for a double backslash is that backlash serves as an escape character in C/C++.
It enables inserting special (control) characters into a string literal. For example: \n means line feed \r is a carriage return, \t is a tab.
Therefore if you want backslash to be interpreted as backslash you have to use two: escape (\) and real (\) ending up with \\.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial