程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Seeking in iostreams,seekingiostreams

Seeking in iostreams,seekingiostreams

編輯:C++入門知識

Seeking in iostreams,seekingiostreams


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.


in seeking to 意思

尋找。。。
 

seeking 跟looking for的不同是?

seek 一般用於抽象意義,有時也表示渴望得到某一具體的東西,是比較正式的書面用法。可構成seek for,seek after等短語,作“尋找,設法得到”等解。其後可接動詞不定式,此時表示“試圖,企圖”。
look for 強調找的過程
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved