将std::string和wchar*连接到wchar*
visual C++ / Concatenate std::string and wchar* to wchar*
我想连接一个std::string
和WCHAR*
,结果应该在WCHAR*
。
我尝试了以下代码
size_t needed = ::mbstowcs(NULL,&input[0],input.length());
std::wstring output;
output.resize(needed);
::mbstowcs(&output[0],&input[0],input.length());
const wchar_t wchar1 = output.c_str();
const wchar_t * ptr=wcsncat( wchar1, L" program", 3 );
我得到了以下错误
错误C2220:警告被视为错误-没有'object'文件生成
错误C2664: 'wcsncat':无法将参数1从'const wchar_t*'转换为'wchar_t*'
如果您调用string.c_str()
来获取原始缓冲区,它将返回一个const指针,表明您不应该尝试更改缓冲区。当然你不应该尝试连接任何东西到它上面。使用第二个字符串类实例,让运行库为您完成大部分工作。
std::string input; // initialized elsewhere
std::wstring output;
output = std::wstring(input.begin(), input.end());
output = output + std::wstring(L" program"); // or output += L" program";
const wchar_t *ptr = output.c_str();
还要记住这一点。一旦"output"超出作用域并销毁,"ptr"将无效。
如文档所述
wchar_t * wcsncat (wchar_t * destination, wchar_t * source, size_t num);将源的第一个num宽字符附加到目标,加上一个终止的空宽字符。返回目的地。(来源:http://www.cplusplus.com/reference/cwchar/wcsncat/)
不能传递const wchar1作为目标,因为函数会修改它并返回它。所以你最好
- 分配一个合适大小的wchar数组
- 复制你的字符串到它
- 使用新分配的wchar数组作为目标调用wcsncat。
然而,我想知道你是否不能仅仅使用字符串来完成你的操作,这更像是c++的方式。(数组是c风格的)
相关文章:
- 当套接字连接断开时检测C/C++Unix
- MacOS通过在莫哈韦"wchar.h"下破碎的自制啤酒发出叮当声
- 无法在windows上使用mingw将sqlite3与c连接
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- OSX蓝牙打开RFCOMMChannelAsync声称已连接,但未建立连接,并且从未调用过委托
- 在 libcurl 连接池中预创建连接
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- libcurl 和 DNS ttl 中的内部连接管理
- 如何应用 libcurl 的持久连接选项
- 提升 ASIO - io_service 不要等待连接到线程
- 需要连接std:string + WCHAR移动gcc代码到Visual c++ 2010