Home C++ Iostream Functions seekg() C++ std::istream::seekg() Function Definition In C++, seekg() moves the get position, the place where the next read starts, in an input stream. The letter g stands for get.
You can jump to an exact place from the start, or move relative to the current place or to the end. It works on files and on string streams. Include <iostream> to use it.
This is a member function of std::istream. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.
Syntax Give a position, or an offset together with a starting point.
in.seekg(5); // 5 characters from the start
in.seekg(-3, std::ios::end); // 3 characters before the end
in.seekg(2, std::ios::cur); // 2 characters forward
in.seekg(0); // back to the beginning Call What it does in.seekg(5)Moves to position 5, counting from the start. in.seekg(-3, std::ios::end)Moves to three characters before the end. in.seekg(2, std::ios::cur)Skips two characters from where the stream is now.
Positions start at 0. The first character is at position 0.
Library declarations istream& seekg(pos_type pos);
istream& seekg(off_type off, ios_base::seekdir dir); Parameters Parameter Description posUsed in the first form. An absolute position from the start, for example 5. offUsed in the second form. A number of characters to move. It can be negative. dirUsed in the second form. The starting point: std::ios::beg (start), std::ios::cur (now), or std::ios::end (end).
Return Value Return Value Description istream&A reference to the stream. If the move was not possible, failbit is set.
C++ Examples 1. Absolute position 2. From the end 3. Relative skip Example 1 — Jump to a Position from the Start Move to position 5 and read the character there.
#include <iostream>
#include <sstream>
int main() {
std::istringstream in("0123456789");
in.seekg(5);
char c = in.get();
std::cout << "character at 5: " << c << '\n';
return 0;
} How It Works The characters are numbered from 0, so the text has positions 0 to 9. Each digit equals its position. seekg(5) sets the read position to 5, so the next read starts at the sixth character.get() returns the character there, which is 5.Example 2 — Read the Last Three Characters A negative offset with std::ios::end counts backwards from the end.
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::istringstream in("0123456789");
in.seekg(-3, std::ios::end);
std::string tail;
in >> tail;
std::cout << "last three: " << tail << '\n';
return 0;
} How It Works std::ios::end means the starting point is after the last character.The offset -3 moves three characters back, to the place of 7. in >> tail reads from there to the end, so tail becomes 789.Example 3 — Skip Forward from the Current Position Read a letter, skip two, then read again. tellg() shows the position.
#include <iostream>
#include <sstream>
int main() {
std::istringstream in("ABCDEFGH");
char first = in.get();
in.seekg(2, std::ios::cur);
char second = in.get();
std::cout << first << " then " << second << '\n';
std::cout << "position now: " << in.tellg() << '\n';
return 0;
} How It Works get() reads A and moves the position to 1.seekg(2, std::ios::cur) moves two characters forward, past B and C, to position 3.The next get() reads D and moves to position 4, which tellg() reports. Frequently Asked Questions What does seekg() do in C++? It moves the read position of an input stream, so the next read starts at the place you choose.
What is the g in seekg? g means get, the input side. The matching function for output streams is seekp, where p means put.
How do I read a file again from the start? Call in.clear() and then in.seekg(0). The first removes eofbit and the second moves back to the first character.
What are beg, cur, and end? They are starting points for a move: std::ios::beg is the start of the stream, cur is the current place, and end is the end.
How do I find the size of a file with seekg? Call in.seekg(0, std::ios::end) and then in.tellg(). The result is the number of bytes in the file.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion