zoukankan      html  css  js  c++  java
  • Seeking in iostreams

    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.

  • 相关阅读:
    Android webView 缓存 Cache + HTML5离线功能 解决
    android 退出系统
    WebView简介(加速加载篇)
    android 处理Back键按下事件
    android 极细线
    [cnblog新闻]历史性时刻:云硬件支出首次高于传统硬件
    Oracle ORDS的简单SQL配置模板
    [cnbeta]华为值多少钱,全世界非上市公司中估值最高的巨头
    其他数据库的restful方式
    [CB]2018全球半导体营收4700亿美元 三星继续碾压英特尔
  • 原文地址:https://www.cnblogs.com/pang1567/p/3990312.html
Copyright © 2011-2022 走看看