第2个回答 2008-07-28
我给你解释下:
[Copy to clipboard] [ - ]CODE:RETURN VALUE
These functions return the number of input items successfully matched
and assigned, which can be fewer than provided for, or even zero in the
event of an early matching failure.
举个例子:
# include < stdio . h >
main ( )
{
char buf [ 20 ] ;
int num ;
FILE * fp =fopen("f1","r+w") ;
if ( fp = = NULL )
{
perror ( "open" ) ;
exit ( 1 ) ;
}
num = fprintf ( fp , "hello" ) ;
printf ( "%d\n" , num ) ;
rewind ( fp ) ;
num = fscanf ( fp , "%s" , buf ) ;
printf ( "%d\n" , num ) ;
write ( 1 , buf , num ) ;
} 执行结果是
5
1
fscanf()函数的返回值不是实际写入缓冲区的字符个数
而是对几个缓冲区成功进行了写 *** 作,就返回这个个数。
程序中只对buf这一个缓冲区进行了scanf *** 作,所以返回值是1
这是比较深的东西,慢慢领悟吧