Skip to content
Related Articles

Related Articles

Improve Article

Set position with seekg() in C++ language file handling

  • Difficulty Level : Medium
  • Last Updated : 24 Oct, 2020

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. It is used in file handling to sets the position of the next character to be extracted from the input stream from a given file. For example : 

Input : "Hello World" 
Output : World

Syntax – There are two syntax for seekg() in file handling :

  1. istream&seekg(streampos position);
  2. istream&seekg(streamoff offset, ios_base::seekdir dir);

Description – 

  • position : is the new position in the stream buffer.
  • 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.

There are 3 direction we use for offset value : 

  • ios_base::beg (offset from the beginning of the stream’s buffer).
  • ios_base::cur (offset from the current position in the stream’s buffer).
  • ios_base::end (offset from the end of the stream’s buffer).

Code –


// Code to demonstrate the seekg function in file handling
#include <fstream>
#include <iostream>
using namespace std;
int main (int argc, char** argv)
    // Open a new file for input/output operations
    // discarding any current 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 A[6];, 5);
    // End the buffer with a null terminating character
    A[5] = 0;
    // Output the contents read from the file and close it
    cout << A << endl;

Output – 


Note – 
If we previously get 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.
This article is contributed by Shivani Baghel. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :