-2

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.

Sophia
  • 57
  • 1
  • 9
  • I am also not sure why it is getting "C". Nothing in your code indicates a reason for that letter. Please provide a [mre] (including declarations for your variables). – JaMiT Jan 07 '21 at 04:24
  • If you received any warnings while compiling please copy/paste those into the question. Chances are you are ignoring one which is telling you precisely what's wrong. – dxiv Jan 07 '21 at 04:24
  • 2
    Take a look at the documentation for `wsprintf`, especially the format string. If you know the buffer is large enough you could use `wcscat` to append to it. Depending on your project settings you might actually be calling `wsprintfA` which would give the behavior you're describing when passed a wide string. – Retired Ninja Jan 07 '21 at 04:24
  • 1
    Obvious problem: you're using the wrong function. `wsprintf` is a weird Windows thing that's mostly a left-over from Windows 3.x. What you want is almost certainly `swprintf` instead. Next problem: you shouldn't really be fiddling with `TCHAR`, which is another weird Windows thing, in this case left over from Windows 95/98. Your code is solely for wide characters anyway, so use `wchar_t`, not `TCHAR`. – Jerry Coffin Jan 07 '21 at 04:27

1 Answers1

4

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);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Or, tags and warnings aside, the OP could be doing a non-Unicode C compile. Then "*the messagebox displays 'C' and nothing else*" indeed. – dxiv Jan 07 '21 at 04:46
  • @dxiv the code shown by the OP would not compile in a non-Unicode build. – Remy Lebeau Jan 07 '21 at 04:47
  • MSVC at least compiles it as C, with the expected C4133 warnings. – dxiv Jan 07 '21 at 04:48
  • I used wsprintf() the way you showed in your example and it worked! Thank you so much! – Sophia Jan 07 '21 at 04:50
  • @dxiv `MessageBox(..., L”New Folder Directory”, ...)` would not compile if `MessageBox()` were mapped to `MessageBoxA()` in a non-Unicode build – Remy Lebeau Jan 07 '21 at 04:52
  • 1
    @RemyLebeau Emphasis on **C** compile. MSVC [does it](https://rextester.com/OGVO62028), and gcc [does it](https://godbolt.org/z/TcY8ET) too. I don't mean to argue with the OP that `wsprintf` fixed it, but I don't see how the original code could have displayed a lone `"C"` in the messagebox if not for an ANSI build being passed wide strings. Not that it really matters anyway ;-) – dxiv Jan 07 '21 at 05:03