C++, regarding fprintf and ofstream

C++, regarding fprintf and ofstream

本文关键字:and ofstream fprintf regarding C++      更新时间:2023-10-16

我已经使用fprintf一段时间了,我想问一个问题。这条fprintf线的等价物是什么:

fprintf(OutputFile, "%s", "SomeStringValue");

使用CCD_ 3?

如何在ofstream中使用"%s"是我真正想知道的。如何获取下一个参数并将其打印为字符串?

你不用它。

等价物本质上是:

std::ofstream x("your_file");
x << "SomeStringValue";
x.close();

去几个参考页中的任何一页上读一下。例如http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/

你不能。你只需将字符串写入流中。

如果您的意思是要为字符串提供一些额外的格式设置(如用空格填充右对齐),那么您可以使用I/O操纵器setfill(' ')(将填充字符设置为空格字符)和setw(length)(设置输出宽度)。如果你想要一些模仿C风格格式字符串语法的东西,你可以使用Boost.format.

std::cout << boost::format("%s") % "SomeStringValue";