ifstream(一)之 eof() 函数——解决读取文件最后一个数据多读一次的方法
来源:互联网 发布:数据挖掘 医疗 编辑:程序博客网 时间:2024/05/21 00:48
原文地址:http://hi.baidu.com/wcmxiaolizi/item/5e304e5da4c5a6b2adc85700
对于文件的操作中主要有几种,无非就是写入、读取罢了。
打开文件时,主要有几种模式 ios::in,ios::binary,ios::app,ios::trunc,ios::ate但并不是每种模式都是适用于ifstream 类的,我们也可以使用组合模式的方式来打开文件.
在读取文件时我们要特别注意一个函数 eof()该函数当文件指针到达文件尾时就返回为true,否则返回false;但是我们用它的时候却会有一些奇怪的事情发生,参考以下代码:
#include <cstdlib>#include <iostream>#include <fstream>using namespace std;int main(int argc, char *argv[]){ const char* filename="temp.txt"; ifstream ifile(filename); if( !ifile ) { cout<<"打开文件失败"<<endl; system("pause"); } char c; while( !ifile.eof() ) { ifile>>c; cout<<c; } cout<<endl; cout << "Press the enter key to continue ..."; cin.get(); return EXIT_SUCCESS;}
多输出了一个 '!' ,难道是eof的设计有误?
如果没有错误为什么符号 '!'输出了两次呢?为了详细的说明问题,我们在while循环中加入一点错误的验证处理机制;
修改成以下代码:
if( ifile.fail() ) cout<<"\n文件读取错误"<<endl; cout<<c;
由此可见在文件读取完符号 '!'之后并没有立即到达文件尾指针。
代码 ifile>>c;读取失败,但是并没有破坏 char c;的数据(内部肯定临时存在一个变量 char temp;)
所以多输出了一个 '!';
在来看另外一个例子,这个例子是操作二进制文件的例子:
#include <cstdlib>#include <iostream>#include <fstream>using namespace std;struct Node{ int key; char mean[10];};typedef Node node;node name[]={ {1,"one"}, {2,"tow"}, {3,"three"}, {4,"fore"}, {5,"five"}, {6,"six"}, {7,"siven"}, {8,"eight"}, {9,"nie"}, {10,"ten"}};int main(int argc, char *argv[]){ const char* filename="temp.dat"; node temp; //写入文件... ofstream ofile(filename,ios::out|ios::binary); if( !ofile ) { cout<<"文件写入失败..."<<endl; system("pause"); } for( int i=0; i < 10; i++ ) { temp=name[i]; ofile.write((char*)&temp,sizeof(temp)); }//end for ofile.close(); //读取文件... ifstream ifile(filename,ios::in|ios::binary); if( !ifile ) { cout<<"读取文件失败..."<<endl; system("pause"); } while( !ifile.eof() ) { ifile.read((char*)&temp,sizeof(temp)); cout<<temp.key<<" "; cout<<temp.mean<<endl; }//end while ifile.close(); cout << "Press the enter key to continue ..."; cin.get(); return EXIT_SUCCESS;}
由此可见确实是eof();函数在读取完最后一组数据时,它还读取了一个什么东西(多多读取了一次),所以在使用该函数的是请增加一个处理,即
if( xxx.fail() )
{
编写你处理方案
...
}
还要在说一句read读取失败时,并没有破坏数据temp(其实现内部肯定有一个临时变量)
编程经验:
常用打开模式(有效组合)组合有
ios::in|ios::binary;//读取以二进制文件格式,并允许使用插入符>>
ios::in|ios::app;//打开文件且添加输出,并允许使用插入符>>
ios::in|ios::ate;//读取文本文件且文件指针一直寻找定位到EOF结束符,并允许使用插入符>>
ios::in|ios::trunc;//打开文件并清空内容,并允许使用插入符>>
...
要注意有一些模式组合是没有意义的(画蛇添足)例如:
ios::in::ios::out;
- ifstream(一)之 eof() 函数——解决读取文件最后一个数据多读一次的方法
- 使用ifstream的get()函数读取文件中的字符后输出时,最后一个字符多输出一次
- ifstream 最后一行多读了一次
- ifstream 最后一行多读了一次
- ifstream 流 判断文件是否结尾的函数eof()
- ifstream 流 判断文件是否结尾的函数eof()
- ifstream中使用eof最后一个字符输出两次
- C/C++ ifstream之eof() 对最后字符读双次的问题
- c++中ifstream一次读取整个文件
- c++中ifstream一次读取整个文件
- c++中ifstream一次读取整个文件
- c++中ifstream一次读取整个文件
- c++中ifstream一次读取整个文件
- c++中ifstream一次读取整个文件
- c++中ifstream一次读取整个文件
- c++中ifstream一次读取整个文件
- c++中ifstream一次读取整个文件
- ifstream eof()函数
- 使用UIScreenEdgePanGestureRecognizer写iOS7侧边栏
- [算法入门经典] 7.5.3 八数码问题 | HDU 1043
- pthread之如何正确的终止正在运行的子线程
- ZOJ Monthly, June 2014——Grouping
- 解决安卓TextView异常换行,参差不齐等问题
- ifstream(一)之 eof() 函数——解决读取文件最后一个数据多读一次的方法
- 浅谈并行编程中的任务分解模式
- 递归删除目录下的所有文件及子目录下所有文件
- 函数指针与指针函数的区别
- truncate 、delete与drop区别
- Performing User-Managed Database-18.5、Restoring Control Files
- python 图的遍历-深度优先和广度优先
- 网络视频传输协议
- OCP 1Z0 051 QUESTION NO: 59