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?
As I found out the two NULL characters stored at the end of the string were the problem. Apparently std::wstring does not care about NULLs like good old C string does. If it thinks a string is 10 characters long, it does not care if some of those 10 characters are NULL characters. If you then append to that string, the additional characters get appended after the 10ths char. If the last characters of the string happen to be NULLs, you get:
Such a string cannot really be used anywhere.
How did I get the NULL characters at the end of the original string? I used wstring.resize() in some other function which pads the string with NULLs. I did this in order to pass &string[0] to a Windows API function expecting a LPWSTR.
Now that I know this does not work I use a true LPWSTR instead. That is a bit more clumsy, but it works. Coming from MFC, I thought I could use std::wstring like CString with its GetBuffer and Release methods.