fprintf、printf 和 sprintf 之间的区别? [英] Difference between fprintf, printf and sprintf?
问题描述
谁能用简单的英文解释一下printf
、fprintf
和sprintf
的区别?
Can anyone explain in simple English about the differences between printf
, fprintf
, and sprintf
with examples?
它在哪个流中?
在阅读C 中的文件处理"时,我真的很困惑这三个.
I'm really confused between the three of these while reading about "File Handling in C".
推荐答案
在 C 中,流"是一种抽象;从程序的角度来看,它只是字节的生产者(输入流)或消费者(输出流).它可以对应于磁盘上的文件、管道、终端或某些其他设备,例如打印机或 tty.FILE
类型包含有关流的信息.通常,您不会直接处理 FILE
对象的内容,您只需将指向它的指针传递给各种 I/O 例程.
In C, a "stream" is an abstraction; from the program's perspective it is simply a producer (input stream) or consumer (output stream) of bytes. It can correspond to a file on disk, to a pipe, to your terminal, or to some other device such as a printer or tty. The FILE
type contains information about the stream. Normally, you don't mess with a FILE
object's contents directly, you just pass a pointer to it to the various I/O routines.
共有三个标准流:stdin
是指向标准输入流的指针,stdout
是指向标准输出流的指针,stderr
code> 是一个指向标准错误输出流的指针.在交互式会话中,这三个通常指的是您的控制台,尽管您可以将它们重定向以指向其他文件或设备:
There are three standard streams: stdin
is a pointer to the standard input stream, stdout
is a pointer to the standard output stream, and stderr
is a pointer to the standard error output stream. In an interactive session, the three usually refer to your console, although you can redirect them to point to other files or devices:
$ myprog < inputfile.dat > output.txt 2> errors.txt
在这个例子中,stdin
现在指向 inputfile.dat
,stdout
指向 output.txt
,并且 stderr
指向 errors.txt
.
In this example, stdin
now points to inputfile.dat
, stdout
points to output.txt
, and stderr
points to errors.txt
.
fprintf
将格式化文本写入您指定的输出流.
fprintf
writes formatted text to the output stream you specify.
printf
相当于编写 fprintf(stdout, ...)
并将格式化文本写入标准输出流当前指向的任何位置.
printf
is equivalent to writing fprintf(stdout, ...)
and writes formatted text to wherever the standard output stream is currently pointing.
sprintf
将格式化文本写入 char
数组,而不是流.
sprintf
writes formatted text to an array of char
, as opposed to a stream.
这篇关于fprintf、printf 和 sprintf 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!