#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    64
    Rep Power
    11

    Replace "\" with "\\"


    Is there a way to replace a \ with \\?

    I tried:
    Code:
    String* myPath="c:\customer\export";
    myPath=myPath->Replace("\","\\");
    But I get this error:
    error C2001: newline in constant
    error C2017: illegal escape sequence
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    New Zealand
    Posts
    28
    Rep Power
    0
    \ is used to specify special chars, eg, \0, \n, \r.

    When you use "\" on its own, the compiler expects there to be a special char after it ( eg, 0, n, r ).

    To specify the actual '\' char, use "\\" which the compiler sees as "\".

    So your code should look like:

    String* myPath="c:\\customer\\export";
    myPath=myPath->Replace("\\,"\\\\");
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    64
    Rep Power
    11
    Thanks worked fine!
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,073
    Rep Power
    1802
    What compiler are you using? GCC (MinGW or Cygwin) accept UNIX style paths for file operations using "/" which may make the code clearer (and portable).

    I do not know if MSVC++ or Borland accept this.

    Clifford
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    64
    Rep Power
    11
    I use visual studio 2003!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    11
    You can also use the @ (operator?):

    String *sString = @"C:\whatever\file.txt";

    is the same as:

    String *sString = "C:\\whatever\\file.txt";

IMN logo majestic logo threadwatch logo seochat tools logo