程序中用到fscanf、fprintf,为了判断异常,需要知道两个函数的返回值含义。
先看fscanf,参考:
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
If a reading error happens or the end-of-file is reached while reading, the proper indicator is set ( or ). And, if either happens before any data could be successfully read, is returned. If an encoding error happens interpreting wide characters, the function sets to EILSEQ.
可以看到,fscanf正常情况下返回从文件中读出的参数个数。比如下面的代码,正常情况下ret=3:
int ret = fscanf (pFile, "%s %d %s", name, &age, addr);
再看fprintf,参考:
On success, the total number of characters written is returned.
If a writing error occurs, the error indicator () is set and a negative number is returned. If a multibyte character encoding error occurs while writing wide characters, is set to EILSEQ and a negative number is returned.
可以看到,fprintf正常情况下返回写入文件的字节数。比如下面的代码:
int ret = fprintf (pFile, "%s %d %s", "zhangzg", 29, "Shanghai");
ret=7+1+2+1+8=19。