scanf()和fscanf()有什么区别?
谢谢。
谢谢。
scanf()
是与 fscanf()
相同的函数,只是第一个参数为 stdin
。
scanf()
,您总是从标准输入读取,而对于 fscanf()
,您需要指定文件输入流。
比较:
int scanf ( const char * format, ... );
int fscanf ( FILE * stream, const char * format, ... );
只需要谷歌一下(我强调):
和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/fscanf/
int fscanf ( FILE * stream, const char * format, ... );
Read formatted data from stream
scanf
函数的文档甚至有一些关于返回值的错误。 - R.. GitHub STOP HELPING ICE从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.
从 SYNOPSIS
中,你或许已经能够猜到了:
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
:)
scanf():从输入/输出到内存(变量)的数据传输 fscanf():从内存(变量)到文件的数据传输。
scanf
变体时,请记得检查返回值的正确性:if (scanf(...) != NUMBER_OF_CONVERSIONS) /* oops */;
- pmg