fgetc, fgets, getc, getchar, ungetc - input of characters and strings
#include <stdio.h>
int getc(FILE *stream);
int fgetc(FILE *stream);
int getchar(void);
//Returns: next character if OK, EOF on end of file or error
getc可以用宏(macro)实现
fgetc一定不用宏实现
getchar等效于getc(stdin)
getc和fgetc之间的差别,意味着三件事:
1. getc的参数一定不能是有副作用的表达式
2. 可以使用fgetc的地址,作为函数指针传入其他function
3. 调用fgetc消耗的时间比getc长
这三个函数都是将unsigned char转换为int,因为EOF通常为负数(定义在stdio.h,有的系统采用-1),int才能装的下。
为了辨别产生EOF是因为文件尾还是错误,我们需要使用ferror,feof来判断
ferror,feof
链接:http://blog.youkuaiyun.com/feather_wch/article/details/50696783
ungetc-push back a character
能向流stream中放回一个字符
#include <stdio.h>
int ungetc(int c, FILE *stream);
尽管ISO C支持放回任何个的字符,但是实现中我们不应该放回超过一个字符。
我们不能push back一个EOF字符。
如果我们达到了文件的结尾,可以放入字符。下一次可以将该字符读取出来,接下来再读取会读取出EOF。
注意
不能使用ungetc将字符放回文件或者设备,因为它们保存在标准IO库的缓冲区中。

本文详细介绍了C语言中的字符输入函数getc、fgetc、getchar和ungetc的使用方法、区别及应用场景,包括如何辨别产生EOF的原因,并提供了如何在实际编程中正确使用这些函数的指导。
4924

被折叠的 条评论
为什么被折叠?



