C ++ Combine 2 Tchar [英] C++ Combine 2 Tchar

查看:188
本文介绍了C ++ Combine 2 Tchar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试合并2 tchar。

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

但我在appdatapath行中遇到错误错误。我如何结合2 tchar?感谢

解决方案

查看 strcat wcscat



如果您使用的是Windows电脑,可以使用 _tcscat 将根据 _UNICODE _MBCS 定义,重定向到正确的函数。



也可以通过将_s附加到函数名来使用安全版本。





注释,你也可以使用snprintf,像这样:

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

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

由于在运行时字符串之前和之后都有字符串文字, p>

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

解决方案

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.

这篇关于C ++ Combine 2 Tchar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆