Search code examples
c++winapiconcatenationslash

How to add slashes to the end of a TCHAR buffer[] using Win32 C++


I am trying to add two slashes "\" to the end of a TCHAR buffer[] using wsprintf() in my Win32 C++ app. The variable "buffer" holds a file path: C:\Users\nayub\Desktop\Folder1\Hello. I want to add two slashes to the end of this file path so did the following code:

wsprintf(filename_buff, buffer, L"\\");
MessageBox(hWnd, filename_buff, L"New Folder Directory", MB_OK | MB_ICONINFORMATION);

However, the messagebox displays "C" and nothing else. I'm not sure why it is getting "C" and not the file path because before the slashes are added, the file path prints out correctly.


Solution

  • You are using wsprintf() incorrectly. It needs to look more like this instead:

    wsprintf(filename_buff, L“%s\\”, buffer);
    

    Or, you can use PathAddBackslash() instead:

    wcscpy(filename_buff, buffer);
    PathAddBackslash(filename_buff);
    

    However, you tagged your question as , so just use std::wstring instead:

    MessageBox(hWnd, (std::wstring(buffer)+L"\\").c_str(), L"New Folder Directory", MB_OK | MB_ICONINFORMATION);