博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fscanf、fprintf的返回值
阅读量:6955 次
发布时间:2019-06-27

本文共 1151 字,大约阅读时间需要 3 分钟。

hot3.png

程序中用到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。

转载于:https://my.oschina.net/zidanzzg/blog/818088

你可能感兴趣的文章
DS博客作业07--查找
查看>>
Codeforces_733C
查看>>
[改善Java代码]动态加载不适合数组
查看>>
Mysql打开日志信息
查看>>
javaScript----Http对象封装
查看>>
Struts2入门
查看>>
纯JS,AJAX
查看>>
数据结构上机1顺序表
查看>>
如何搭建自己的SPRING INITIALIZR server
查看>>
JDK5-注解
查看>>
定义表单控件的id和name注意点
查看>>
UILabel里字体带下划线
查看>>
Ios开发之多线程编程——NSThread
查看>>
linux下网站压力测试工具webbench
查看>>
滑动关闭activity
查看>>
Android Studio撤销与SVN的关联
查看>>
【规范】alibaba编码规范阅读
查看>>
shell中的特殊变量和函数传参
查看>>
使用pdf2htmlEX将pdf文件转为html
查看>>
天鹅会面
查看>>