C ++,关于fprintf和ofstream [英] C++, regarding fprintf and ofstream
问题描述
我已经使用了fprintf
一段时间了,我想问一个问题.这条fprintf
行等效于什么:
I've been using fprintf
for a while now and I'd like to ask a question. What is the equivalent of this fprintf
line:
fprintf(OutputFile, "%s", "SomeStringValue");
使用ofstream
吗?
我真的很想知道如何使用ofstream
中的%s".如何接受下一个参数并将其打印为字符串?
How to use the "%s" in ofstream
is what I'd really like to know. How to take the next argument and print it as a string?
推荐答案
您不能.您只需将字符串写入流中.
You can't. You just write the string to the stream.
如果您想为字符串提供一些其他格式(如使用空格填充右对齐),则可以使用I/O操纵器setfill(' ')
(将填充字符设置为空格字符)和setw(length)
(设置输出的宽度).如果您想模仿C样式格式字符串的语法,可以使用 Boost.format .
If you mean you want to provide some additional formatting to the string (like right justified with space padding), then you can use the I/O manipulators setfill(' ')
(set the fill character to be the space character) and setw(length)
(setting the width of the output). If you want something that mimics the syntax of the C style format strings, you can use Boost.format.
std::cout << boost::format("%s") % "SomeStringValue";
这篇关于C ++,关于fprintf和ofstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!