当前位置:网站首页>文件结尾
文件结尾
2022-07-29 05:08:00 【Ryan菲特】
文件结尾
无论操作系统实际使用何种方法监测文件结尾,在C语言中,用 getchar() 读取文件监测到文件结尾时将返回一个特殊的值,即 EOF( end of file 的缩写)。scanf()函数检测到文件结尾时也返回EOF。
关键是要记住EOF是一个值,标志着监测到文件结尾,并不是文件中找得到的符号。
注意几点:
1.不用定义EOF,因为stdio.h中已经定义过了。
2.不用担心EOF的实际值,因为EOF在stdio.h中用#define 预处理指令定义,可直接使用,不必再编写代码假定EOF为某值。
3.变量ch的类型从char 变为int ,因为char类型的变量只能表示0~255的无符号整数,但是EOF的值是-1。还好,getchar() 函数实际返回值的类型是int,所以它可以读取EOF字符。
4.由于getchar()函数的返回类型是int,如果把getchar()的返回值赋给char类型的变量,一些编译器会警告可能丢失数据。
5.ch是整数不会影响putchar(),该函数仍然会打印等价的字符。
6.使用该程序进行键盘输入,要设法输入EOF字符。不能只输入字符EOF,也不能只输入-1(输入-1会传送两个字符:一个连字符和一个数字1)。正确的方法是,必须找出当前系统的要求。
看ecoh.c和ecoh_eof.c的区别:
#include <stdio.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '#')
putchar(ch);
return 0;
}
ecoh.c的输出结果:
Hello,there.I would
Hello,there.I would
like a #3 bag of potatoes.
like a
以下是ecoh_eof.c的程序:
#include <stdio.h>
int main(void)
{
int ch;
while((ch = getchar()) != EOF)
putchar(ch);
return 0;
}
ecoh_eof.c的输出结果
hello!
hello!
How are you?
How are you?
zzzz
zzzz
^Z
上面输出结果中的^Z(是键盘上的Ctrl+Z),UNIX和Linux系统中在一行开始处按下Ctrl+D会传输文件结尾信号。许多微型计算机系统都把一行开始处的Ctrl+Z识别为文件结尾信号。
流的概念
流(stream)是一个实际输入或输出映射的理想化数据流。这以为这不同属性和不同种类的输入,由属性更统一的流来表示。打开文件的过程就是把流与文件相关联,而且读写都通过流来完成。
边栏推荐
- How mongodb inserts, deletes and updates documents
- Arfoundation starts from scratch 3- create an arfoundation project
- 阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂
- Xiaolu Inn - Trailer
- The latest tank battle 2022 full development notes-1
- 7.1-default-arguments
- D3d Shader Instruction
- Button for QT custom switch effect
- 研发效能生态完整图谱&DevOps工具选型必看
- QT series - Installation
猜你喜欢
JS (foreach) return cannot end the function solution
MySQL many to many relationship, grouping and splicing to query multiple data to one data
京东云金秋上云特惠进行中!扫码参与活动
6.2 function-parameters
osg3.6.5编译freetype失败
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
Self join and joint query of MySQL
C语言求字符串的长度
Architecture analysis of three-tier project and parameter name injection of construction method
200 多家 ISV 入驻!阿里云计算巢发布一周年
随机推荐
NumPy基础
How mongodb inserts, deletes and updates documents
Is Huatai Securities an AA level securities company? How about this company? Is it safe to open an account?
自定义Qml控件:ImageButton
Qt版的贪食蛇游戏项目
C语言求字符串的长度
C语言数组入门到精通(数组精讲)
ARFoundation入门教程10-平面检测和放置
Database course design of online assistant teaching platform for high school chemistry
321, Jingdong Yanxi × Nlpcc 2022 challenge starts!
Qml类型:State 状态
MySQL sorts the queried result set according to the specified sequence
Getting started with arfoundation tutorial 10- plane detection and placement
Custom QML control: imagebutton
365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com
365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held
Learn the first program of database