C++ seekp 函数文件流跳转功能产生数据覆盖问题解决
2012-03-13 17:03
435 查看
近来,使用C++输出一组数据到文件(*.txt). 需要在输出全部结果后,返回到文件头重新写入一组数据.
使用seekp函数定位到相关位置:
seekp函数有两种情况,主要是:
basic_ostream<_Elem, _Tr>& seekp( pos_type _Pos ); basic_ostream<_Elem, _Tr>& seekp( off_type _Off, ios_base::seekdir _Way );
其中:
_Pos
The position in the stream.
_Off
The offset relative to _Way.
_Way
One of the ios_base::seekdir enumerations.
The seekdir type as : static const seekdir beg, cur, end;
两个情况,分别表示:
1 跳转到指定位置 : pos_type _Pos - 字节为跳转单位;
2 跳转到指定位置,并微调偏移: _way 是需要跳转的位置,有三种即beg-序列(数组,文件流,文件)的开始,
-Off 跳转后位置的偏移量,不可以越界.
问题: 跳转后,如果跳转到流中间位置,继续打印或者输出新的数据,会出现覆盖旧数据的情况.
解决办法是:再要插入新数据的位置处,返回插入之前,提前写入相关的类型相同数目的数据并最好以回车换行做标记.这样跳转到新位置后再直接写入数据覆盖事先写入到该位置的无用数据.
示例代码: 以下代码,先输入一些数据到txt文件,然后返回到文件起始位置,插入一些新的数据,为了避免覆盖原有数据,提前在起始位置写入了与要插入的数据相同数目相同同类型(或者其类型长度大于要插入类型长度)的数据.
使用seekp函数定位到相关位置:
seekp函数有两种情况,主要是:
basic_ostream<_Elem, _Tr>& seekp( pos_type _Pos ); basic_ostream<_Elem, _Tr>& seekp( off_type _Off, ios_base::seekdir _Way );
其中:
_Pos
The position in the stream.
_Off
The offset relative to _Way.
_Way
One of the ios_base::seekdir enumerations.
The seekdir type as : static const seekdir beg, cur, end;
两个情况,分别表示:
1 跳转到指定位置 : pos_type _Pos - 字节为跳转单位;
2 跳转到指定位置,并微调偏移: _way 是需要跳转的位置,有三种即beg-序列(数组,文件流,文件)的开始,
-Off 跳转后位置的偏移量,不可以越界.
问题: 跳转后,如果跳转到流中间位置,继续打印或者输出新的数据,会出现覆盖旧数据的情况.
解决办法是:再要插入新数据的位置处,返回插入之前,提前写入相关的类型相同数目的数据并最好以回车换行做标记.这样跳转到新位置后再直接写入数据覆盖事先写入到该位置的无用数据.
示例代码: 以下代码,先输入一些数据到txt文件,然后返回到文件起始位置,插入一些新的数据,为了避免覆盖原有数据,提前在起始位置写入了与要插入的数据相同数目相同同类型(或者其类型长度大于要插入类型长度)的数据.
void FileOperBack(){ int iNumCounter=0; unsigned int recoData=0; int recoveryData[6]={0,1,2,3,4,5}; ofstream testFout("G:\\WorkSpace\\FileOperation\\testFileBackRead.txt"); for (int i = 0; i < 6; i++) { testFout<<recoData<<" "; } testFout<<endl<<endl; //testFout<<recoData<<" "<<recoData<<" "<<recoData; for (int i = 0; i < 6 ; i++) { for (int j = 0; j < 6; j++) { testFout<<i*j<<" "; testFout<<flush; iNumCounter++; } testFout<<endl; testFout<<flush; } testFout.seekp( 0, ios_base::beg ); testFout<<"47414 47414 "<<iNumCounter<<endl<<endl; //for (int i = 0; i < 6; i++) //{ // testFout<<recoveryData[i]<<" "; //} //testFout<<endl; testFout.close(); testFout.clear(); }
相关文章推荐
- 提取数据用strpos函数比较,预期和实际不符问题解决
- 编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时会产生Additional information: 阅读器关闭时尝试调用 Read 无效问题,解决方法与解释
- 编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时会产生Additional information: 阅读器关闭时尝试调用 Read 无效问题,解决方法与解释
- 解决SQLSERVER在还原数据时出现的“FILESTREAM功能被禁用”问题
- 解决springMVC在浏览器请求数据时产生的乱码问题
- php使用fputcsv()函数csv文件读写数据的方法(解决中文乱码问题)
- 解决$.Ajax等返回数据windows.open()跳转页面遭到浏览器拦截的问题
- 用分类汇总的功能解决无序数据的统计问题
- access中,有无将文本类型的数据格式转化成数值型的函数?(解决 order by 的非数字排序问题)
- 2个自定义的PHP in_array 函数,解决大量数据判断in_array的效率问题
- vue项目中做编辑功能传递数据时遇到问题的解决方法
- Python 读Dict数据的方法,解决key 不在dict的问题,get()函数,setdefault()函数
- 2个自定义的PHP in_array 函数,解决大量数据判断in_array的效率问题
- access中,有无将文本类型的数据格式转化成数值型的函数?(解决 order by 的非数字排序问题)
- Hibernate使用原生SQL多表查询时字段名相同导致查询数据覆盖问题解决办法
- 按如下函数原型用函数编程解决如下的日期转换问题(要求考虑闰年的问题): 输入某年某月某日,计算并输出它是这一年的第几天。 /* 函数功能: 对给定的某年某月某日,计算它是这一年的第几天 函数参数:
- QT 信号槽connect中解决自定义数据类型或数组作为函数参数的问题——QT qRegisterMetaType 注册MetaType——关键:注册自定义数据类型或QMap等容器类
- JavaScript跳转页面/Action并传递中文参数[解决js超链接传递过程中产生的中文乱码问题]
- 解决SQLSERVER在还原数据时出现的“FILESTREAM功能被禁用”问题
- 2个自定义的PHP in_array 函数,解决大量数据判断in_array的效率问题