0

I have a string which is something like "c:\x\y\z"

I want it in the form "c://x//y//z"

I tried using stdString.replace("\","//"); but it doesn't work.

Any suggestions?

sloth
  • 99,095
  • 21
  • 171
  • 219
Rohit Kabra
  • 55
  • 1
  • 9
  • 1
    Can u try using stdString.replace("\\","//"); have a look at http://stackoverflow.com/questions/2417588/escaping-a-c-string – Felix Christy Jun 14 '12 at 07:27
  • Thanks.. a step ahead.. But this error comes up error C2782: 'void std::replace(_FwdIt,_FwdIt,const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous 1> C:\Programme\Microsoft Visual Studio 10.0\VC\include\algorithm(1317) : see declaration of 'std::replace' 1> could be 'int' 1> or 'char' – Rohit Kabra Jun 14 '12 at 07:50
  • @RohitKabra my guess is you haven't read the answers. Have you checked out the link I posted? Have you read how `replace` works? – Luchian Grigore Jun 14 '12 at 07:51
  • @RohitKabra: do you really want two slash characters (ie., two `'/'`) separating each path component? – Michael Burr Jun 14 '12 at 08:01
  • Yeah. Want to use it for TempPath.. and In turn for CreateDirectory (TempPath.c_str(), NULL); – Rohit Kabra Jun 14 '12 at 08:16
  • If you plan to use '/' as the separator you only need one, not two. `c:/temp/somefile.txt` – Retired Ninja Jun 14 '12 at 09:02

3 Answers3

5

If your string is "c:\x\y\z", there are no \ in your string. \ denotes an escape character. Change your string to "c:\\x\\y\\z".

Also, note how replace works - http://www.cplusplus.com/reference/string/string/replace/

I don't think you can replace one character '\\' with two "//" directly. (I might be proven wrong).

Alternative:

std::stringstream ss;
for ( int i = 0 ; i < str.size() ; i++ )
{
    if ( str[i] == '\\' )
        ss << "//";
    else
        ss << str[i];
}
str = ss.str();
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • @Rohit Kabra, what he means is that using `\x` for example, makes that `\` act as an escape character instead of the character `\`. So what u must do is use `\\x` instead. Unless if that string is being read in from an external source, in which case i believe this effect gets taken care of (it only affects hard-coded / constant values). – JWL Jun 14 '12 at 07:29
  • the string is being read from the external source and i need to convert is as i m appending it later on "//w". – Rohit Kabra Jun 14 '12 at 07:46
  • 1
    @RohitKabra have you checked out the link I provided? I think you're misusing string.replace. – Luchian Grigore Jun 14 '12 at 07:50
  • @LuchianGrigore : I did. But then in stdString.replace("\\","//"); "//" has two characters.. so what do i do? – Rohit Kabra Jun 14 '12 at 07:53
  • std::replace(stdString.begin(), stdString.end(), '\\', '//'); This is what i used.. – Rohit Kabra Jun 14 '12 at 07:57
  • 1
    @RohitKabra well, `std::replace` is hardly the same as `string::replace`. – Luchian Grigore Jun 14 '12 at 08:00
  • @LuchianGrigore : It would be great if you could give me the statement.. i have tried since a long time and stuck here.. – Rohit Kabra Jun 14 '12 at 08:03
  • @RohitKabra I don't think you can directly replace one character with two characters in the string. You can use `std::replace(tdString.begin(), stdString.end(), '\\', '/');`. – Luchian Grigore Jun 14 '12 at 08:05
  • Well what i notice is that "\\ is not read in the string itself.. I printed all the ascii characters of the string and "\\" doesnt get printed.. So all the conversion leads to nothng.!! – Rohit Kabra Jun 14 '12 at 09:32
  • @RohitKabra `\\` will get printed as `\` because `\` marks an escape character. – Luchian Grigore Jun 14 '12 at 09:34
  • @RohitKabra just like `'\n'` doesn't get printed as-is, but pastes a new line. – Luchian Grigore Jun 14 '12 at 09:34
  • I converted it to ascii and then trying to print it. It should print the number 92. Wont it? Its as if the string doesnt contain "\" – Rohit Kabra Jun 14 '12 at 09:39
0
stdString.replace("\\","\/\/"); ?
Sergey Vakulenko
  • 1,655
  • 18
  • 23
  • 1
    `'/'` does not need to be escaped (though it will work on many compilers). More importantly, `std::string::replace()` doesn't have a form that will match what you've posted (or do what it seems you expect it to do). – Michael Burr Jun 14 '12 at 07:56
0

if you hard coded the filepath in your source code then you may change value to "c:\\x\\y\\z"

ndeverge
  • 21,378
  • 4
  • 56
  • 85
Riskhan
  • 4,434
  • 12
  • 50
  • 76