Seeking in iostreams

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值