C++ Combine 2 Tchar

backspace picture backspace · Feb 5, 2014 · Viewed 13.7k times · Source

I'm trying to combine 2 tchar.

char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);
TCHAR* appdatapath ="C:\\Users\\"+username+"\\AppData";

But I get error error at appdatapath line. How can I combine 2 tchar? Thanks

Answer

Eric Fortin picture Eric Fortin · Feb 5, 2014

Have a look at strcat and wcscat. You can't add char pointer with char array.

If you are on a windows machine, you can use _tcscat which will redirect to the right function to use depending on _UNICODE and _MBCS defines.

Might want to use the safe versions as well by appending _s to the function name.


As pointed in the comments, you can also use snprintf like so:

const size_t concatenated_size = 256;
char concatenated[concatenated_size];

snprintf(concatenated, concatenated_size, "C:\\Users\\%s\\AppData", username);

Since you have string literals before and after the runtime string, it is probably a better approach.