Seeking in iostreams

C++文件流定位操作
本文介绍使用C++中的istream和ostream进行文件流位置移动的方法,包括绝对定位和相对定位两种方式,并通过示例代码展示了如何获取文件大小、定位到文件特定位置及读取部分内容。

Each type of iostream has a concept of where its “next” character will come from (if it’s an istream) or go (if it’s an ostream). In some situations you may want to move this stream position. You can do it using two models: One uses an absolute location in the stream called the streampos; the second works like the Standard C library functions fseek( )for a file and moves a given number of bytes from the beginning, end, or current position in the file.

The streamposapproach requires that you first call a “tell” function: tellp( )for an ostreamor tellg( )for an istream. (The “p” refers to the “put pointer” and the “g” refers to the “get pointer.”) This function returns a streamposyou can later use in the single-argument version of seekp( )for an ostreamor seekg( )for an istream, when you want to return to that position in the stream.

The second approach is a relative seek and uses overloaded versions of seekp( )and seekg( ). The first argument is the number of bytes to move: it may be positive or negative. The second argument is the seek direction:

ios::beg  From beginning of stream
ios::cur  Current position in stream
ios::end  From end of stream

Seeking.cpp

   1:  #include "../require.h"
   2:  #include <iostream>
   3:  #include <fstream>
   4:  using namespace std;
   5:   
   6:  int main() {
   7:    ifstream in("Seeking.cpp");
   8:    assure(in, "Seeking.cpp"); // File must already exist
   9:    in.seekg(0, ios::end); // End of file
  10:    streampos sp = in.tellg(); // Size of file
  11:    cout << "file size = " << sp << endl;
  12:    in.seekg(-sp/10, ios::end);
  13:    streampos sp2 = in.tellg();
  14:    in.seekg(0, ios::beg); // Start of file
  15:    cout << in.rdbuf(); // Print whole file
  16:    in.seekg(sp2); // Move to streampos
  17:    // Prints the last 1/10th of the file:
  18:    cout << endl << endl << in.rdbuf() << endl;
  19:  } ///:~

This program picks a file name off the command line and opens it as an ifstream. assert( ) detects an open failure. Because this is a type of istream, seekg( )is used to position the “get pointer.” The first call seeks zero bytes off the end of the file, that is, to the end. Because a
streamposis a typedeffor a long, calling tellg( )at that point also returns the size of the file, which is printed out. Then a seek is performed moving the get pointer 1/10 the size of the file – notice it’s a negative seek from the end of the file, so it backs up from the end. If you try to seek positively from the end of the file, the get pointer will just stay at the end. The streamposat that point is captured into sp2, then a seekg( )is performed back to the beginning of the file so the whole thing can be printed out using the streambufpointer produced with rdbuf( ). Finally, the overloaded version of seekg( )is used with the streampos sp2to move to the previous position, and the last portion of the file is printed out.

转载于:https://www.cnblogs.com/pang1567/p/3990312.html

MATLAB代码实现了一个基于多种智能优化算法优化RBF神经网络的回归预测模型,其核心是通过智能优化算法自动寻找最优的RBF扩展参数(spread),以提升预测精度。 1.主要功能 多算法优化RBF网络:使用多种智能优化算法优化RBF神经网络的核心参数spread。 回归预测:对输入特征进行回归预测,适用于连续值输出问题。 性能对比:对比不同优化算法在训练集测试集上的预测性能,绘制适应度曲线、预测对比图、误差指标柱状图等。 2.算法步骤 数据准备:导入数据,随机打乱,划分训练集测试集(默认7:3)。 数据归一化:使用mapminmax将输入输出归一化到[0,1]区间。 标准RBF建模:使用固定spread=100建立基准RBF模型。 智能优化循环: 调用优化算法(从指定文件夹中读取算法文件)优化spread参数。 使用优化后的spread重新训练RBF网络。 评估预测结果,保存性能指标。 结果可视化: 绘制适应度曲线、训练集/测试集预测对比图。 绘制误差指标(MAE、RMSE、MAPE、MBE)柱状图。 十种智能优化算法分别是: GWO:灰狼算法 HBA:蜜獾算法 IAO:改进天鹰优化算法,改进①:Tent混沌映射种群初始化,改进②:自适应权重 MFO:飞蛾扑火算法 MPA:海洋捕食者算法 NGO:北方苍鹰算法 OOA:鱼鹰优化算法 RTH:红尾鹰算法 WOA:鲸鱼算法 ZOA:斑马算法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值