Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::wstring: concatenation with + has no effect

The solution is probably obvious, but I do not see it. I have this simple C++ code:

// Build the search pattern
// sPath is passed in as a parameter into this function
trim_right_if(sPath, is_any_of(L"\\"));
wstring sSearchPattern = sPath + L"\\*.*";

My problem is that the + operator has no effect (checked in debugger). The string sSearchPattern is initialized to the value of sPath only.

Notes: sPath is a wstring.

Example of what I want to achieve:

sSearchPattern -> C:\SomePath\*.*

More Info:

When I look at sPath in the debugger, I see two NULL characters after the last character. When I look at sSearchPattern, the "\*.*" is appended, but after the two NULL characters. Any explanation for that?

like image 408
Helge Klein Avatar asked Dec 05 '22 19:12

Helge Klein


1 Answers

This should work, and indeed works for me on VS2010, SP1:

#include <iostream>
#include <string>

int main()
{
    const std::wstring sPath = L"C:\\SomePath";
    const std::wstring sSearchPattern = sPath + L"\\*.*";

    std::wcout << sSearchPattern << L'\n';

    return 0;
}

This prints

C:\SomePath\*.*

for me.

like image 134
sbi Avatar answered Dec 29 '22 13:12

sbi