0

I currently using C string headers and C++ and have hit a problem. I have a long path:

C:\bla\bla\bla\bla

I need to change the backslashes to double backslashes so that my OS_CopyFile() function can read it properly but I don't know how?

I get my path using:

CHAR* szValueBuf = NULL;
DWORD cchValueBuf = 0;
UINT uiStat =  MsiGetProperty(hInstall, TEXT("OriginalDatabase"), TEXT(""), &cchValueBuf);

if (ERROR_MORE_DATA == uiStat)
{
    ++cchValueBuf; 
    szValueBuf = new TCHAR[cchValueBuf];
    if (szValueBuf)
    {
        uiStat = MsiGetProperty(hInstall, TEXT("OriginalDatabase"), szValueBuf, &cchValueBuf);
    }
}
if (ERROR_SUCCESS != uiStat)
{
    if (szValueBuf != NULL) 
        delete[] szValueBuf;
    return ERROR_INSTALL_FAILURE;
}
Natalie Carr
  • 3,707
  • 3
  • 34
  • 68
  • 3
    Where is your string coming from? – robert Aug 27 '12 at 12:46
  • 1
    Your `OS_CopyFile` function seems mighty broken. – Konrad Rudolph Aug 27 '12 at 12:46
  • 5
    When hardcoding paths in the source code, you can just as well use forward slashes and save you the problem. `"C:/bla/bla/bla/bla"` – Bo Persson Aug 27 '12 at 12:47
  • 2
    Remember that the double backslashes are only necessary for literal (i.e., hard-coded) strings; if you get the string from anything other than your own code, there should be nothing that you need to do to pass it onto system calls. Now, if you're going to hand it over to something that does further string parsing (like, say, a RegEx engine), THEN you might need to escape them... – Andy Hopper Aug 27 '12 at 12:48
  • I will edit my question now and tell you where I get my path from. – Natalie Carr Aug 27 '12 at 13:02
  • @KonardRudolph my OS_copyfile is this: `OS_CopyFile("szValueBuf", "C:\\TEMP\\product.ini",0);` can you see a problem? – Natalie Carr Aug 27 '12 at 13:16
  • @NC1: I think he[Konrad] means the function itself is broken, not your usage of it. Did you not write it? Where does it come from? Google gives me nothing. – Benjamin Lindley Aug 27 '12 at 13:31
  • @BenjaminLindley, I've realized my error, `szValueBuf` should not have been encased in speech marks..as it took it literally. Silly me..Thanks for all your help – Natalie Carr Aug 27 '12 at 13:38

3 Answers3

1

Why not copy the original string, one character at a time, and when you see a backslash then you just append an extra backslash to the copy?

But as other has noted, if the string is not e.g. hardcoded in the source, then you most likely won't need to do this.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

If you have the string you show in a C variable at runtime, there's no risk that the backslashes will be changed.

Backslashes in C strings are only special to the C compiler while parsing source code, it will replace e.g. "\n" with a string that contains the single linefeed character. This replacement only happens in actual string literals, never in memory at run-time.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

You have misunderstood how backslashes work in C++. Your string only has single backslashes. In a string literal you must use two backslashes in your code to get one backslash in the string at runtime.

OS_CopyFile(szValueBuf, "C:\\TEMP\\product.ini",0);

There are only two backslashes in the string above. You've invented for yourself a problem that doesn't exist.

john
  • 85,011
  • 4
  • 57
  • 81
  • Yes i realized that, I was having issues that my `CopyFile` wasn't working and put in the slashes thinking that was the problem. My problem was that actually i put my first variable in speech marks and the function referred to it literally. Read somewhere about having double slashes, If I can pinpoint where I will inform them. Thanks – Natalie Carr Aug 27 '12 at 14:05