string replace problem

Hi !

I'm new in c++, and this is my first post... Please be clement :)

I need to build a little programm which receive a Windows path (by command line arguments) and which returns this path changing backslashes to slashes.

I did it like in this topic : http://www.cplusplus.com/forum/beginner/80181/ . I made this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <iostream>

using namespace std;

int main (int argc, char *argv[])
  {
  int i;                                  // compteur d'arguments de ligne de commande
  std::string chaineFinale="/cygdrive/";  // variable à retourner avec le bon Path pour RSync

  for (i=1; i<argc; ++i)
    {
    std::string tempStr = argv[i];
    if (i==1) chaineFinale = chaineFinale+tempStr;
    else chaineFinale = chaineFinale+" "+tempStr;
    }

  // Remplacer les backslashes par des slashes
  chaineFinale.replace(chaineFinale.begin(),chaineFinale.end(),'\\','/');

  std::cout << chaineFinale;

  return 0;
  }


When I compile (with g++ provided by MinGW), there is no error. But when I call my soft :

 
mysoft.exe c:\my path\to


I have only lot of slashes like that :

 
////////////////////////////////////////////////////////////////////////////////////////// 


It's very strange because :
- if I "cout" my string just before the "replace", my string contains only "c:\my path\to"
- if I send "c:\my path\to" or "c:\my path very long\to", the answer of my programm as still the same number of slashes

Does anyone have an idea ?

Many thanks,

JC
I have some idea.:)

Include header <algorithm> and instead of

chaineFinale.replace(chaineFinale.begin(),chaineFinale.end(),'\\','/');

use

replace(chaineFinale.begin(),chaineFinale.end(),'\\','/');
Last edited on
Many thanks Vlad, it works very fine now !!!

Do you have an explanation about why "string replace" doesn't work, and "algorithm replace" works ??

This afternoon, I will read algorithm full doc, it seems very usefull :)

Many (many) thanks again !
You need to read the description of the std::string member function replace and meanings of its parameters and all will be clear.
Ouups, yes you're right. I read too quickly the post I mentionned (http://www.cplusplus.com/forum/beginner/80181/), the solution was wrong from the start. I shouldn't trust everyone.

Many thanks again !
Topic archived. No new replies allowed.