Turn Path String.

Posted on 1999-12-19
Medium Priority
Last Modified: 2010-04-01
  How to turn "c:\windows\system" path string into
"c:\\windows\\system" directly, in order to finish
some FILE operation.
  Is there a MFC function?
Question by:henry3
  • 5
  • 3
LVL 11

Expert Comment

ID: 2293883
Use CString::Replace("\\", "\\\\");

I'm not sure if you have the right reasons for doing this, but that's how you would do it.

..B ekiM

Author Comment

ID: 2294026
   Thanks for your advice.
   But I cannot find CString::Replace() at all.
   When I use API SHFileOperation with the
parameter LPCSTR  pFrom & LPCSTR  pTo, the
format should be
"Driver1:\\XXX\\XXX\0Driver2:\\XXX\0" in order
to do some multiFiles operation.
   So I wanna change the string format, is it
necessary? And how to do so?
   Looking forward your suggestion.

Expert Comment

ID: 2294063
Ypu have han LPCSTR variable (assume lpszPath and strlen is nPathLen) with contains "Driver1:\XXX\XXX\0Driver2:\XXX\0" where you want to replace the "\" with "\\".

If my uderstanding is right then this is what you can do.

CString    szReplaceString(lpszPath);
lpszPath = (LPCTSTR) szReplaceString.GetBuffer(nPathLen);

Now the resulting lpszPath contains "Driver1:\\XXX\\XXX\\0Driver2:\\XXX\\0".

Hope this is what you needed.
If you are using VC just include the following code in you file to get the CString functions.

#include <afx.h>

Feel free to report any problems. My mail id is abdij_b@hotmail.com
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

LVL 11

Expert Comment

ID: 2295546
> szReplaceString.Replace("\","\\");

That code won't compile.

If you don't have CString::Replace(), you're not using VC++ 6.0.  Is that true?  If so, you'll need to write your own version of the function--which isn't hard.

..B ekiM
LVL 11

Expert Comment

ID: 2296360
Even if you fix abdij's code so it compiles, you'll find it doesn't work.

The Replace() function will fix the backwhacks in the first substring (up until the first '\0'), but won't touch anything afterwards.

The calls to GetBuffer() and ReleaseBuffer() don't do a darned thing except waste some memory and time.

I've never seen the syntax you mention for pTo in SHFILESTRUCT, so I don't know if you need double-whacks or not. You only need to escape the whacks if you're providing a string constant--that's for the C++ language, not the API.

..B ekiM


Author Comment

ID: 2296894
   Unfortunatly, I'm using VC++ 5.0 .
   The syntax I mentioned for pTo in SHFILESTRUCT, appeared in tech artical
I've seen.
   And I found if you didn't change "c:\xxx\xxx.txt" to "c:\\xxx\\xxx.txt",
some error result will come.
   \0 is the separator of two directory.
   If it isn't cerrect and necessary, how do I do?
LVL 11

Accepted Solution

mikeblas earned 0 total points
ID: 2298640
If you code a string literal, then you need to have both backslashes.  Like this:

   char* pstr = "c:\\xxx\\xxx.txt";

If you're getting the string from another source (like the user typing it in), you don't need to double the backslashes.

..B ekiM

Author Comment

ID: 2307131
I have another idea.
LVL 11

Expert Comment

ID: 2315253
> I have another idea.


..B ekiM

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question