我希望这将是一个非常基本的问题,这可能是由于我对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());