Link to home
Start Free TrialLog in
Avatar of sbartok
sbartok

asked on

Converting \ to \\

Ok, no i've got my win95 directory in a buffer and now I need to convert the \ in the path to \\ .... how ?
Avatar of nietod
nietod

answer coming.
I assume you want to convert all the slashes to double slashes.  If not let me know.

The followig copies a string from Src to Dst and converts ever single slash to a double slash.

char Src[128]; // Buffer with single slash string.
char Dst[128]; // Buffer to receive double slash string.

const char *SrcPtr = Src;
char *DstPtr = Dst;

while(true)
{
   char CurChr = *SrcPtr++;

   *DstChr++ = CurChr;
    if (DstChr == '\\') // If character was a slash, then
      *DstChr++ = CurChr; // Output another slash.
    else if (!CurChr) // If at end of string, then
       break;
}
Was wrong with that?  If you don't like or don't understand an answer you should add a comment so that it is clear to the expert (and other experts) what the problem is.
Avatar of sbartok

ASKER

Thank you, (note : this is a mfc app)  i'll try this.
could you give me your email if i have any ?s ?
would be cool !
if you write it as a answer again, you get 10 points :-)
Avatar of sbartok

ASKER

The answer was correct ! exactly what i needed !
Avatar of sbartok

ASKER

The answer was correct ! exactly what i needed !
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
An exclamation point?  As if it is a suprise that it works.  Now my feelings are hurt. :-)
Avatar of sbartok

ASKER

Ok,
yeah, i know about mcf/c++ relations :-) i just wanted to tell you.
i also understand your email stuff (boy, i get abour 20messages per day )

Avatar of sbartok

ASKER

my email is sacha@dillingen.baynet.de
Avatar of sbartok

ASKER

hmm, it doesn't work...
my code looks like  :


char Src[128]; // Buffer with single slash string.
char Dst[128]; // Buffer to receive double slash string.
GetWindowsDirectory(Src,128);
const char *SrcPtr = Src;
char *DstPtr = Dst;

while(true)
{
char CurChr = *SrcPtr++;

/*line 208*/ *DstChr++ = CurChr;
if (DstChr == '\\') // If character was a slash, then
/*line 210*/ *DstChr++ = CurChr; // Output another slash.
else if (!CurChr) // If at end of string, then
break;
}


and i get the following compiler errors :
E:\Applications\CopyManager\CopyManagerDlg.cpp(208) : error C2065: 'DstChr' : undeclared identifier
E:\Applications\CopyManager\CopyManagerDlg.cpp(208) : error C2100: illegal indirection
E:\Applications\CopyManager\CopyManagerDlg.cpp(208) : error C2106: '=' : left operand must be l-value
E:\Applications\CopyManager\CopyManagerDlg.cpp(210) : error C2100: illegal indirection
E:\Applications\CopyManager\CopyManagerDlg.cpp(210) : error C2106: '=' : left operand must be l-value

any idea ?


Avatar of sbartok

ASKER

please send me the code per e@mail. thanx
There were a few typos.  Try this.

     char Src[128]; // Buffer with single slash string.
     char Dst[128]; // Buffer to receive double slash string.
     GetWindowsDirectory(Src,128);
     const char *SrcPtr = Src;
     char *DstPtr = Dst;

     while(true)
     {
     char CurChr = *SrcPtr++;

     *DstPtr++ = CurChr;   /// changed
     if (CurChr == '\\') // If character was a slash, then /// changed
        *DstPtr++ = CurChr; // Output another slash.  //// changed
     else if (!CurChr) // If at end of string, then
     break;
     }

I marked the lines that were changed.  If it okay with you, I prefer to post the correct information here.  That way there is a permenant record for EE.  I only use e-mail in extreme cases.  
Search for the ascii value of "\" then place another into the string behind it. this workes well when asking for a path from the user, strcmp or step over the string using a pointer increment.
You know, this was answered about 6 months ago.
Avatar of sbartok

ASKER

cool guys, thanx anyway :-)