当前位置:网站首页>End of document
End of document
2022-07-29 05:28:00 【Ryan fitter】
End of file
No matter what method the operating system actually uses to monitor the end of the file , stay C In language , use getchar() When reading the file and monitoring the end of the file, a special value will be returned , namely EOF( end of file Abbreviation ).scanf() The function also returns... When it detects the end of the file EOF.
The key is to remember EOF It's a value , It indicates that the end of the file is monitored , Not the symbol found in the file .
Pay attention to some points. :
1. Don't define EOF, because stdio.h Has been defined in .
2. Never mind EOF The real value of , because EOF stay stdio.h of use #define Preprocessing instruction definition , Can be used directly , There is no need to write code to assume EOF For a certain value .
3. Variable ch Type from char Turn into int , because char Variables of type can only represent 0~255 Of unsigned integers , however EOF The value of is -1. not so bad ,getchar() The type of the actual return value of the function is int, So it can read EOF character .
4. because getchar() The return type of the function is int, If you put getchar() The return value of is assigned to char Variable of type , Some compilers warn of possible data loss .
5.ch Being an integer does not affect putchar(), This function will still print equivalent characters .
6. Use this program for keyboard input , Try to enter EOF character . You can't just enter characters EOF, You can't just enter -1( Input -1 Two characters will be transmitted : A hyphen and a number 1). The right way is , We must find out the requirements of the current system .
see ecoh.c and ecoh_eof.c The difference between :
#include <stdio.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '#')
putchar(ch);
return 0;
}
ecoh.c Output result of :
Hello,there.I would
Hello,there.I would
like a #3 bag of potatoes.
like a
Here are ecoh_eof.c The program :
#include <stdio.h>
int main(void)
{
int ch;
while((ch = getchar()) != EOF)
putchar(ch);
return 0;
}
ecoh_eof.c Output result of
hello!
hello!
How are you?
How are you?
zzzz
zzzz
^Z
In the above output results ^Z( It's on the keyboard Ctrl+Z),UNIX and Linux Press... At the beginning of a line in the system Ctrl+D The end of file signal will be transmitted . Many microcomputer systems start with Ctrl+Z Recognized as a signal at the end of the file .
The concept of flow
flow (stream) Is an idealized data flow of actual input or output mapping . This means that different attributes and different kinds of inputs , Represented by a more uniform flow of attributes . The process of opening a file is to associate the stream with the file , And reading and writing are done through streams .
边栏推荐
- 牛客网编程题—【WY22 Fibonacci数列】和【替换空格】详解
- OCCT学习002-----环境搭建
- 365 day challenge leetcode1000 question - distance between bus stops on day 038 + time-based key value storage + array closest to the target value after transforming the array and + maximum value at t
- 167. Sum of two numbers II - enter an ordered array
- 数千个数据库、遍布全国的物理机,京东物流全量上云实录 | 卓越技术团队访谈录
- 【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
- Cryengine5 shader debugging
- 365天挑战LeetCode1000题——Day 038 公交站间的距离 + 基于时间的键值存储 + 转变数组后最接近目标值的数组和 + 有界数组中指定下标处的最大值
- 哈夫曼树以及哈夫曼编码在文件压缩上的应用
- Cmu15-213 malloc lab experiment record
猜你喜欢
随机推荐
More than 200 ISVs have settled in! The first anniversary of Alibaba cloud computing nest
CryEngine3 调试Shader方法
C语言数组入门到精通(数组精讲)
数组学习之入门简单题 两数之和
水一篇图的拓扑排序
Preemptive appointment | Alibaba cloud shadowless cloud application online conference appointment opens
指针
365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions
Day 5
一维数组练习
C语言 一级指针
省市区三级联动(简单又完美)
【C语言系列】— 一道递归小题目
关于局部变量
Cryengine5 shader debugging
Occt learning 002 - environment construction
Differences between texture2d and texture2dproj under webgl1.0
京东云金秋上云特惠进行中!扫码参与活动
Best practices for elastic computing in the game industry
阿里云联合鼎捷软件发布云上数字工厂解决方案,实现云MES系统本地化部署