我没有太多使用 C++ 的经验。相反,我在 C# 中工作得更多,因此,我想通过与我将在那里做的事情相关联来问我的问题。我必须生成特定格式的字符串,我必须将其传递给另一个函数。在 C# 中,我可以通过下面的简单代码轻松生成字符串。
string a = "test";
string b = "text.txt";
string c = "text1.txt";
String.Format("{0} {1} > {2}", a, b, c);
通过生成上述这样的字符串,我应该能够在 system()
中传递它。但是, system
只接受 char*
我在 Win32 C++
(不是 C++/CLI)上,并且不能使用 boost
因为它会包含太多的项目本身很小的所有文件。 Something like sprintf()
looks useful to me, but sprintf
does not accept string
as the a
, b
and c
参数。有什么建议可以在我的程序中生成这些格式化的字符串以传递给系统吗?
原文由 user1240679 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以将
sprintf
与std::string.c_str()
结合使用。c_str()
返回一个const char*
并与sprintf
一起使用:或者,如果您知道大小,您可以使用预先分配的
char
数组: