C语言 scanf() 和 fscanf() 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5533926/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:15:53  来源:igfitidea点击:

what are the differences between scanf() and fscanf()?

c

提问by phpNewComer

what are the differences between scanf() and fscanf()?

scanf() 和 fscanf() 之间有什么区别?

thanks

谢谢

回答by caf

scanf()is exactly identical to fscanf()with stdinas the first argument.

scanf()fscanf()stdin第一个参数完全相同。

回答by Gustav Larsson

For scanf()you always read from standard input and for fscanf()you specify the file input stream.

因为scanf()您总是从标准输入中读取并为fscanf()您指定文件输入流。

Compare:

相比:

int  scanf ( const char * format, ... );
int fscanf ( FILE * stream, const char * format, ... );

回答by configurator

Just google it (emphasis mine):

只需谷歌一下(强调我的):

http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

int  scanf ( const char * format, ... );

Read formatted data from stdin

http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

int  scanf ( const char * format, ... );

标准输入读取格式化数据

And

http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/

int fscanf ( FILE * stream, const char * format, ... );

Read formatted data from stream

http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/

int fscanf ( FILE * stream, const char * format, ... );

流中读取格式化数据

回答by sarnold

From the third paragraph of fscanf(3)manpage:

fscanf(3)联机帮助页的第三段:

   The scanf() function reads input from the standard input stream
   stdin, fscanf() reads input from the stream pointer stream, and
   sscanf() reads its input from the character string pointed to by
   str.

You might have been able to guess that from the SYNOPSIS:

您可能已经能够从以下内容中猜到SYNOPSIS

   int scanf(const char *format, ...);
   int fscanf(FILE *stream, const char *format, ...);

:)

:)

回答by Saravana

scanf() : data transfer from I/O to memory(variables) fscanf() : data transfer form memory(variables) to file.

scanf() :从 I/O 到内存(变量)的数据传输 fscanf() :从内存(变量)到文件的数据传输。