将字符串与 sprintf() 函数一起使用时出现问题
Trouble using a string with the sprintf() function
我希望这将是一个非常基本的问题,这可能是由于我对Win32 API C++的许多功能缺乏了解。
无论如何,我在sprintf()
功能方面遇到了一些麻烦。我将它与这样的字符串一起使用没有问题:
//memory is a void pointer that maps a file to shared memory
sprintf((char *)memory, "Shared memory message"); //Write to shared memory
但是当我尝试使用 string
变量时,它不起作用......
sprintf((char *)memory, str_var); //Write to shared memory
我收到一个错误,上面写着:no suitable conversion function from "std::string" to "const char *" exists
.我什至无法通过类型转换来解决此问题,就像我对memory
所做的那样。
这似乎很不一致。我做错了什么,我如何给它一个它会接受的价值?
你需要sprintf
,你需要传递一个const char*
:
- http://linux.die.net/man/3/sprintf
所以例如
sprintf((char *)memory, str_var.c_str());
相关文章:
- 如何将GTest与CMake一起使用?遵循谷歌指南时的链接问题
- 在浮点精度成为一个问题之前,可以将多少个浮点值加在一起
- 将cpp_redis pub/sub与条件变量一起使用时出现问题
- 在本地安装OpenMPI的问题与CUDA一起使用
- 与自定义命名空间一起使用时实现 std::error_category、名称解析问题
- 与lambda一起使用虚拟继承在初始化列表中捕获此问题的GCC错误
- 将 cplex 与 netbeans c++ 一起使用,链接器问题
- 将双缓冲位图与 AlphaBlend 一起使用时出现的问题
- 有关将智能指针与 Winsock 的 addrinfo 结构一起使用的问题
- QT TreeView问题与排序模型一起使用
- 将模板气泡排序与结构数组一起使用时出现问题
- 将 boost/thread headers 与 clang++ (Windows) 一起使用时出现问题
- 将 Botan 与 Qt 一起使用时遇到问题
- 与私人成员一起上课.这个代码出了什么问题
- 将指针与动态数组一起使用时出现问题
- 将问题与多个文件链接在一起
- Clang:将绑定或mem_fn与字符串::c_str和转换一起使用时出现问题
- 将get_value与 Boost 的属性树一起使用时遇到问题
- 将字符串与 sprintf() 函数一起使用时出现问题
- 将矢量与函数一起使用,指针问题