C语言 fprintf、printf 和 sprintf 之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4627330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between fprintf, printf and sprintf?
提问by mr_eclair
Can anyone explain in simple English about the differences between printf, fprintf, and sprintfwith examples?
谁能用简单的英语解释一下printf, fprintf, 和sprintf与例子之间的区别?
What stream is it in?
它在什么流中?
I'm really confused between the three of these while reading about "File Handling in C".
在阅读“C 中的文件处理”时,我真的很困惑这三个。
回答by John Bode
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 FILEtype contains information about the stream. Normally, you don't mess with a FILEobject's contents directly, you just pass a pointer to it to the various I/O routines.
在 C 中,“流”是一种抽象;从程序的角度来看,它只是字节的生产者(输入流)或消费者(输出流)。它可以对应于磁盘上的文件、管道、终端或某些其他设备,例如打印机或 tty。该FILE类型包含有关流的信息。通常,您不会FILE直接弄乱对象的内容,您只需将指向它的指针传递给各种 I/O 例程。
There are three standard streams: stdinis a pointer to the standard input stream, stdoutis a pointer to the standard output stream, and stderris 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:
共有三种标准流:stdin是指向标准输入流stdout的指针,是指向标准输出流stderr的指针,是指向标准错误输出流的指针。在交互式会话中,这三个通常指的是您的控制台,尽管您可以将它们重定向到其他文件或设备:
$ myprog < inputfile.dat > output.txt 2> errors.txt
In this example, stdinnow points to inputfile.dat, stdoutpoints to output.txt, and stderrpoints to errors.txt.
在此示例中,stdinnow 指向inputfile.dat、stdout指向output.txt和stderr指向errors.txt。
fprintfwrites formatted text to the output stream you specify.
fprintf将格式化文本写入您指定的输出流。
printfis equivalent to writing fprintf(stdout, ...)and writes formatted text to wherever the standard output stream is currently pointing.
printf相当于fprintf(stdout, ...)将格式化文本写入标准输出流当前指向的任何位置。
sprintfwrites formatted text to an array of char, as opposed to a stream.
sprintf将格式化文本写入 的数组char,而不是流。
回答by Moo-Juice
printfoutputs to the standard output stream (stdout)
printf输出到标准输出流 ( stdout)
fprintfgoes to a file handle (FILE*)
fprintf转到文件句柄 ( FILE*)
sprintfgoes to a buffer you allocated. (char*)
sprintf转到您分配的缓冲区。( char*)
回答by Rubal
printf("format", args) is used to print the data onto the standard output which is often a computer monitor.
printf("format", args) 用于将数据打印到通常是计算机显示器的标准输出上。
sprintf(char *, "format", args) is like printf. Instead on displaying the formated string on the standard output i.e. a monitor, it stores the formated data in a string pointed to by the char pointer (the very first parameter). The string location is the only difference between printf and sprint syntax.
sprintf(char *, "format", args) 就像 printf。代替在标准输出(即监视器)上显示格式化字符串,它将格式化数据存储在由字符指针(第一个参数)指向的字符串中。字符串位置是 printf 和 sprint 语法之间的唯一区别。
fprintf(FILE *fp, "format", args) is like printf again. Here instead on displaying the data on the monitor, or saving it in some string, the formated data is saved on a file which is pointed to by the file pointer which is used as the first parameter to fprintf. The file pointer is the only addition to the syntax of printf.
fprintf(FILE *fp, "format", args) 又像 printf 了。这里不是在监视器上显示数据,或将其保存在某个字符串中,格式化数据保存在文件中,该文件由文件指针指向,该文件指针用作 fprintf 的第一个参数。文件指针是 printf 语法的唯一补充。
If stdoutfile is used as the first parameter in fprintf, its working is then considered equivalent to that of printf.
如果将stdout文件用作 fprintf 中的第一个参数,则认为其工作与 printf 等效。
回答by VGE
printf(...)is equivalent to fprintf(stdout,...).
printf(...)相当于fprintf(stdout,...)。
fprintfis used to output to stream.
fprintf用于输出到流。
sprintf(buffer,...)is used to format a string to a buffer.
sprintf(buffer,...)用于将字符串格式化为缓冲区。
Note there is also vsprintf, vfprintfand vprintf
注意还有vsprintf,vfprintf和vprintf
回答by Maxim Egorushkin
You can also do very useful things with vsnprintf() function:
您还可以使用 vsnprintf() 函数做非常有用的事情:
$ cat test.cc
#include <exception>
#include <stdarg.h>
#include <stdio.h>
struct exception_fmt : std::exception
{
exception_fmt(char const* fmt, ...) __attribute__ ((format(printf,2,3)));
char const* what() const throw() { return msg_; }
char msg_[0x800];
};
exception_fmt::exception_fmt(char const* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(msg_, sizeof msg_, fmt, ap);
va_end(ap);
}
int main(int ac, char** av)
{
throw exception_fmt("%s: bad number of arguments %d", *av, ac);
}
$ g++ -Wall -o test test.cc
$ ./test
terminate called after throwing an instance of 'exception_fmt'
what(): ./test: bad number of arguments 1
Aborted (core dumped)
回答by Amit Vasava
printf
打印输出
- printf is used to perform output on the screen.
- syntax =
printf("control string ", argument ); - It is not associated with File input/output
- printf 用于在屏幕上执行输出。
- 语法 =
printf("control string ", argument ); - 它与文件输入/输出无关
fprintf
打印文件
- The fprintf it used to perform write operation in the file pointed to by FILE handle.
- The syntax is
fprintf (filename, "control string ", argument ); - It is associated with file input/output
- 它用于在 FILE 句柄指向的文件中执行写操作的 fprintf。
- 语法是
fprintf (filename, "control string ", argument ); - 它与文件输入/输出相关联
回答by kavamsi12
fprintfThis is related with streams where as printfis a statement similar to fprintfbut not related to streams, that is fprintfis file related
fprintf这与流有关,其中与流printf类似fprintf但与流无关的语句,即fprintf文件相关
回答by Fahad Ali
sprintf: Writes formatted data to a character string in memory instead of stdout
sprintf:将格式化数据写入内存中的字符串而不是 stdout
Syntax of sprintf is:
sprintf 的语法是:
#include <stdio.h>
int sprintf (char *string, const char *format
[,item [,item]…]);
Here,
这里,
String refers to the pointer to a buffer in memory where the data is to be written.
字符串指的是指向内存中要写入数据的缓冲区的指针。
Format refers to pointer to a character string defining the format.
格式是指指向定义格式的字符串的指针。
Each item is a variable or expression specifying the data to write.
每个项目都是一个变量或表达式,指定要写入的数据。
The value returned by sprintf is greater than or equal to zero if the operation is successful or in other words the number of characters written, not counting the terminating null character is returned and returns a value less than zero if an error occurred.
如果操作成功,则 sprintf 返回的值大于或等于零,或者换句话说,写入的字符数,不计算终止空字符,如果发生错误,则返回小于零的值。
printf: Prints to stdout
printf:打印到标准输出
Syntax for printf is:
printf 的语法是:
printf format [argument]…
The only difference between sprintf() and printf() is that sprintf() writes data into a character array, while printf() writes data to stdout, the standard output device.
sprintf() 和 printf() 之间的唯一区别是 sprintf() 将数据写入字符数组,而 printf() 将数据写入标准输出设备 stdout。

