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

6
scanf()和fscanf()有什么区别?
谢谢。

在使用任何 scanf 变体时,请记得检查返回值的正确性:if (scanf(...) != NUMBER_OF_CONVERSIONS) /* oops */; - pmg
5个回答

11

scanf() 是与 fscanf() 相同的函数,只是第一个参数为 stdin


9
对于 scanf(),您总是从标准输入读取,而对于 fscanf(),您需要指定文件输入流。

比较:

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

2

cplusplus.com不是一个非常可靠的参考资料。如果我没记错的话,它对scanf函数的文档甚至有一些关于返回值的错误。 - R.. GitHub STOP HELPING ICE
@R:谢谢,知道这些对我很有帮助。您能向我指出更可靠的参考来源吗? - configurator

0

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, ...);

:)


-3

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


请在您的回答中更加详细地描述。目前它的质量不是很高。 - Scott Barta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接