CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 1999
    Posts
    4

    How can i convert string "c:\a\b" to "c:\\a\\b" ?



    Hi

    How can i convert string "c:\a\b" to "c:\\a\\b" ?

    thanx

  2. #2
    Join Date
    Apr 1999
    Posts
    15

    Re: How can i convert string "c:a" to "c:\a\b" ?



    Hi

    just create a loop iterating through the characters of the string, copy each

    character from the string into a new string and whenever you find the '\' character,

    plug in another '\' character into the new string.

    HTH

    Julius

  3. #3
    Join Date
    Mar 1999
    Posts
    22

    Re: This is not simple, isn't this.



    Hi.


    I had a same problem to do it before.

    But I solved it.

    If you have "c:\a\b", you must cut this string like "c:", "a" and "b".

    After this, CString str = CString("c:") + "\\" + "\\";

    You will get "c:\\" and apply to "a" and combines them.


    If you try "\", the complier says error -> new line ...


    Does anyone has better idea?

    Regards.

    -Masaaki Onishi-



  4. #4
    Join Date
    May 1999
    Location
    Oregon, USA
    Posts
    302

    Re: How can i convert string "c:a" to "c:\a\b" ?



    As usual, everyone has their own prefered method.

    Here is the pseudcode for mine.

    This will have problems with a CString


    testchar = startofstring

    while( testchar is not null )

    {

    if( testchar is a slash )

    {

    move entire string right one place (including null)

    insert another slash

    advance testchar two places (for two slashes)

    }

    else

    advance testchar one place

    }


    If I gave you real code it would spoil your fun



  5. #5
    Join Date
    Mar 1999
    Posts
    4

    Re: How can i convert string "c:a" to "c:a" ?



    Thank u all for such fast responding

    actualy i am trying to get TCHAR type filename from GetSaveFile

    function(which is giving me "c:\a\b\") and give it to

    wmfDC.Create("c:\\a\\b\\blah.wmf").

    I understood all ur suggestsions...thought there is any special function

    for this...

    Thanx

    Samanta forever ;-)

  6. #6
    Join Date
    May 1999
    Location
    Oregon, USA
    Posts
    302

    Re: How can i convert string "c:a" to "c:a" ?



    At that level, single backslashes should work.

    Are they being 'escaped' ?

    A possibly easier thing to do would be to switch them all to

    forward slashes. This little loop will do it.

    while( TRUE )

    {

    char * cptr = strchr( filename, '\\' );

    if( cptr )

    *cptr = '/';

    else

    break;

    }


    Most function will accept forward slashes in path names.

    Maybe I should say, many functions will.



  7. #7
    Join Date
    Mar 1999
    Posts
    22

    Re: I got it. Only 4 lines- Replace()



    Hi.

    You are lucky.

    CString filename = "C:\Masaaki\Masaaki\Masaaki.txt";

    CString one = "\\";

    CString two = CString("\\&quot + "\\";

    int n = filename.Replace(one, two);

    // filename is now "C:\\Masaaki\\Masaaki\\Masaaki.txt";

    I just check this by the debugger, so probably this is OK.

    Replace is only avaliabe for VC6.0.


    Hope for help.

    Masaaki Onishi-







  8. #8
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: How can i convert string "c:a" to "c:\a\b" ?





    CString str("c:\a\b");

    str.Replace("\", "\\");


    Sally

  9. #9
    Join Date
    Mar 1999
    Posts
    22

    Re: You is missing this.



    Hi.


    I posted this above.

    Can you explain this. So I use "\\" and "\\" + "\\"


    D:\MyProjects\VC\ReBars\MyDialog.cpp(58) : error C2017: illegal escape sequence

    D:\MyProjects\VC\ReBars\MyDialog.cpp(58) : error C2017: illegal escape sequence

    D:\MyProjects\VC\ReBars\MyDialog.cpp(58) : error C2001: newline in constant

    D:\MyProjects\VC\ReBars\MyDialog.cpp(61) : error C2143: syntax error : missing ')' before 'tag::id'

    D:\MyProjects\VC\ReBars\MyDialog.cpp(61) : error C2661: 'Replace' : no overloaded function takes 1 parameters

    Error executing cl.exe.



  10. #10
    Join Date
    Apr 2001
    Posts
    13

    Re: How can i convert string "c:\a\b" to "c:\\a\\b" ?

    Hi
    str = "c:\a\b"
    str.Replace("\","\\\\");

    i think this will work.

    bye

    Mahesh Kumar C D

  11. #11
    Join Date
    Apr 2001
    Posts
    13

    Re: How can i convert string "c:\a\b" to "c:\\a\\b" ?

    Hi
    str = "c:\a\b"
    str.Replace("\","\\\\");


    or u need to use
    4 backslashes there to get 2 backslashes.


    i think this will work.

    bye

    Mahesh Kumar C D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured