Do more with
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.
#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();
}
Premium Content
You need an Expert Office subscription to comment.Start Free Trial