文件结束符 End Of File
来源:互联网 发布:js有几种数据类型 编辑:程序博客网 时间:2024/06/05 08:14
先看下面的程序
#include<iostream>#include<fstream>using namespace std;int main(){ofstream outfile("test.txt");outfile<<"abcdefg";outfile.close();char c;ifstream infile("test.txt");while(!infile.eof()){infile>>c;cout<<c;}system("pause");return NULL;}
其输出是" abcdefgg ",可以发现输出了两个“ g ”,这是什么原因呢?
1、文件指针
当打开一个文件时,对文件输入流来说,指针指向的是当前读取字符串的下一位,每读一位,指针就往下移一位。
2、关于EOF
大家通常误认为文件结束符EOF是从文件读取的一个字符,其实不然,EOF被定义为int型的一个负数(比如-1)。EOF也不是文件中实际存在的内容,而是文件流的状态标志。在C++中,当读取文件失败才产生EOF,所以当输出第一“ g ”时,产生EOF,输出第二个“ g ”时,读取到EOF,循环结束。
3、解决方案
将判断循环结束的方式改为“ infile.peek() != EOF ”。
infile.peek() 其返回值是一个char型的字符,其返回值是指针指向的当前字符,但它只是观测,指针仍停留在当前位置,并不后移。
修改程序如下: 其输出结果是”abcdefg“
#include<iostream>#include<fstream>using namespace std;int main(){ofstream outfile("test.txt");outfile<<"abcdefg";outfile.close();char c;ifstream infile("test.txt");while(infile.peek() != EOF){infile>>c;cout<<c;}system("pause");return NULL;}
注:程序修改之后,若输入字符串最后有一个空格符或换行符,则输出结果是"abcdefgg",因为输出"g"时,文件还没有结束(还有一个空格符或换行符),而peek()函数不会删除之前缓存区里的字符"g",所以循环继续,输出的还是"g"。 0 0
- 文件结束符 End Of File
- Fortran读取文件遇到 end-of-file
- Premature end of file
- Premature end of file
- unexpected end of file
- 解压cpio文件:cpio: premature end of file
- unexpected end of file found
- bash: unexpected end of file
- unexpected end of file found
- SAXParseException : Premature end of file
- premature end of file错误
- DOM4J:Premature end of file
- EOF 键盘输入end of file
- Premature end of JPEG file
- vc++ resource.h文件出现“fatal error RC1004: unexpected end of file found”问题
- fatal error C1010: unexpected end of file while looking for precompiled header directive 头文件设置
- 关于cpio: 文件非正常终止(cpio: premature end of file)
- vc++ resource.h文件出现“fatal error RC1004: unexpected end of file found”问题
- AOJ-AHU-OJ-59 09年3月选拔赛第4题
- AOJ-AHU-OJ-12 Box of Bricks
- AOJ-AHU-OJ-592 神奇的叶子
- 用管理员身份登录
- AOJ-AHU-OJ-61 Lake Counting(递归)
- 文件结束符 End Of File
- Install Monitoring Systems
- 统计所有单词出现的次数:二叉数
- 关于java中try catch finally的执行顺序
- python+eclipse 开发环境配置 http://pydev.org/updates
- LeetCode 150 — Evaluate Reverse Polish Notation(C++ Java Python)
- Apache+Tomcat集群配置
- Eclipse ADT 使用空格替代Tab键缩进整块整块代码
- C++ COM编程生成随机GUID值