fprintf 在 C++ 中如何工作?

发布于 2024-08-03 08:20:46 字数 1435 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谁的年少不轻狂 2024-08-10 08:20:46

"test %d %d 255/r" 告诉后面将是参数(它们在那里:255, 255)并且它们应该是整数类型。并且它们将代替 %d 被放置。

结果,您将在文件中获得字符串 test 255 255 255

有关更多信息,请阅读 std::fprintf 参考。

"test %d %d 255/r" tells that after it will be arguments (and they are there: 255, 255) and they are expected to be of integer type. And they will be placed instead of %d.

In result you'll get string test 255 255 255 in your file.

For more infrormation read std::fprintf reference.

知足的幸福 2024-08-10 08:20:46

第一个参数是文件句柄,第二个参数是格式化字符串,之后的参数数量可变,具体取决于第二个参数中使用的格式说明符的数量。

检查文档,它包含您询问的所有信息。

The first parameter is a file handle, the second is a formatting string, and after that there is a variable number of arguments depending on how many format specifiers you used in your 2nd parameter.

Check the documentation, it contains all of the information you are asking.

堇色安年 2024-08-10 08:20:46

它是文件流的格式化输出。它的工作方式与 printf 类似,不同之处在于 printf 始终输出到 stdout。如果您编写

fprintf(stdout, "test %d %d 255\n", 255, 255);

它,它将与 printf 等效项相同。

它的第二个参数是格式字符串。格式字符串包含格式说明符,例如 %s%d%x。您的包含两个 %d。每个格式说明符必须在 fprintf 中具有相应的参数。您的有两个 %d 说明符,因此有两个数字参数:

fprintf(outfile, "Here are two numbers: %d, %d", 4, 5);

同样,您可以使用字符串说明符 (%s)、十六进制说明符 (%x)或 long/long long int 说明符(%ld%lld)。以下是其中的列表: http://www.cppreference.com/wiki/c /io/printf。请注意,它们对于所有 C 格式的 I/O 函数(如 sprintf 和 scanf)都是相同的。

另外,在您原来的示例中,“/r”将直接打印“/r”。看起来您正在尝试执行回车符(“\r”)。

it's a formatted output to a file stream. It works like printf does, the difference is that printf always outputs to stdout.If you wrote

fprintf(stdout, "test %d %d 255\n", 255, 255);

it would be the same as the printf equivalent.

The second argument to it is the format string. The format string contains format specifiers, like %s, %d, %x. Yours contains two %ds. Each format specifier must have a corresponding argument in fprintf. Yours has two %d specifiers, so there are two numerical arguments:

fprintf(outfile, "Here are two numbers: %d, %d", 4, 5);

likewise, you could use string specifiers (%s), hexadecimal specifiers (%x) or long/long long int specifiers (%ld, %lld). Here is a list of them: http://www.cppreference.com/wiki/c/io/printf. Note that they are the same for all of the C formatted i/o functions (like sprintf and scanf).

Also, in your original example, "/r" will just literally print "/r". It looks like you were trying to do a carriage return ("\r").

流年里的时光 2024-08-10 08:20:46

它类似于 printf,它只是将输出打印到文件中。

It's similar to printf, it just prints the output to a file.

风渺 2024-08-10 08:20:46

第二个参数是格式字符串。任何附加参数都是格式字符串中说明符的参数(在本例中为 %d)。查看 http://www.cppreference.com/wiki/c/io/printf< /a> 很好地介绍了 printf 风格的函数。

The second argument is the format string. Any additional arguments are the parameters to the specifier in the format string (in this case, %d). Check out http://www.cppreference.com/wiki/c/io/printf for a good intro to printf-style functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文