请输入您要查询的百科知识:

 

词条 Seekg
释义

  1. Example

  2. Example

{{Orphan|date=August 2013}}

In the C++ programming language, seekg is a function in the iostream library (part of the standard library) that allows you to seek to an arbitrary position in a file. This function is defined for istream class - for ostream class there's a similar function Seekp (this is to avoid conflicts in case of classes that derive both istream and ostream, such as iostream).

istream& seekg ( streampos position );

istream& seekg ( streamoff offset, ios_base::seekdir dir );

  • position is the new position in the stream buffer. This parameter is an object of type streampos.
  • offset is an integer value of type streamoff representing the offset in the stream's buffer. It is relative to the dir parameter.

dir is the seeking direction. It is an object of type ios_base::seekdir that can take any of the following constant values:

  1. ios_base::beg (offset from the beginning of the stream's buffer).
  2. ios_base::cur (offset from the current position in the stream's buffer).
  3. ios_base::end (offset from the end of the stream's buffer).

Note: If you have previously got an end of file on the stream, seekg will not reset it but will return an error in many implementations.

- use the clear() method to clear the end of file bit first. This is a relatively common mistake and if seekg() is not performing as expected, it is wise to clear the fail bit, as shown below.

Example

  1. include
  2. include

using namespace std;

int main (int argc, char** argv)

{
    // Open a new file for input/output operations discarding any current    // content in the file (assumes a length of zero on opening)    fstream myFile("test.txt", ios::in | ios::out | ios::trunc);        // Add the characters "Hello World" to the file    myFile << "Hello World";        // Seek to 6 characters from the beginning of the file    myFile.seekg(6, ios::beg);        // Read the next 5 characters from the file into a buffer    char buffer[6];    myFile.read(buffer, 5);        // End the buffer with a null terminating character    buffer[5] = 0;        // Output the contents read from the file and close it     cout << buffer << endl;        myFile.close();

}

Example

  1. include
  2. include
  3. include

using namespace std;

int main (int argc, char** argv)

{
    // Creates a new ifstream object and associates it with a file passed in via the parameters    // The object is then checked to see if the end-of-file has been reached, if the file has data    // then this will be false    ifstream myFile(argv[1], ios::in);    cout << myFile.eof() << endl;        // Continuously loops over each line of the file until the end of the file            // Again outputs the end-of-file status for the stream, this time the answer will be true    cout << myFile.eof() << endl;        // Seeks to the very beginning of the file, clearing any fail bits first (such as the end-of-file bit)    myFile.clear();    myFile.seekg(0, ios::beg);        // Outputs the status of the stream again, the result will be false    cout << myFile.eof() << endl;        myFile.close();

}

2 : C++|Articles with example C++ code

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/11 0:17:56