seekg() function in C++
In this article, we will discuss seekg() function in C++ with its syntax, parameter, example, and applications.
In C++, seekg() is a member function of the istream class, which is part of the Standard Library and is used for input stream operations. It lets you control where the read pointer is located within the input stream. Seekg(), which stands for "seek get", is a function used to place the get pointer (also known as the read pointer) inside the input stream.
Syntax:
It has the following syntax:
istream& seekg (streampos pos);
istream& seekg (streamoff off, ios_base::seekdir dir);
Parameters:
It has the following syntax parameters:
Pos: It signifies a specific location inside the stream. Pos belong to the streampos data type, an alternative for the ios_base::pos_type data type. It details the get pointer's new location concerning the input stream's starting point.
Off: It denotes a position relative to the stream. Off belongs to the data type streamoff, an alternative for ios_base::off_type. It defines how many bytes the get pointer should advance in the dir-specified direction.
dir: It stands for the direction the get pointer should go. The enumeration type of dir is ios_base::seekdir, which can take one of the following values:
ios_base::beg (from the beginning of the stream)
ios_base::cur (from the get pointer's current position)
ios_base::end (from the stream's end)
Methods of seekg() function:
There are two methods for utilizing the C++ seekg() method. These are as follows:
1. istream&seekg(streampos position)
- The position is the parameter that determines where the pointer is changed.
- First, open a new file for I/O operations according to the algorithm. For instance, the file's name will be "tae.txt". Let the file's current location be 3. After that, include some characters in the file, such as "example". Now, seek 3 characters from where the pointer is placed, i.e., to 6.
- tae.seekg(3) ;
- Now, write the content from the file into the buffer. After that, close the buffer.
2. istream&seekg(streamoff offset, ios_base::seekdir dir)
- A pointer's offset is the value it skips and forwards to. The pointer's initial direction is indicated by the word dir.
- First, open a new file for I/O operations according to the algorithm. For illustration, make this time's filename "tae2.txt". Add some characters to the file once more. Let it be "tutorial and example" this time. Find the first 3 characters, i.e., to 3—now.
- tae2.seekg(3, ios::beg) ;
- Now, write the content from the file into the buffer. After that, close the buffer.
Return value:
seekg() function modifies the pointer in the file and returns the istream object (\*this).
Now, we take a sample program to understand the use of seekg() function in C++:
Code:
#include <fstream>
#include <iostream>
using namespace std;
int main (int argc, char** argv) {
//opening file and making operations
fstream File("tae.txt", ios::in | ios::out );
File << "tutorial and example";
//assigning direction to beginning
File.seekg(13, ios::beg);
char F[20];
File.read(F, 14);
//printing output
cout <<F<< endl;
//closing file
File.close();
}
Output:
![seekg() function in C++](https://static.tutorialandexample.com/cpp/seekg-function-in-cpp1.png)
Applications of seekg() function in C++:
When working with input streams (such as files or strings), the seekg() function in C++ can be quite helpful. Here are a few typical uses for seekg():
Handling Binary Data:
When reading binary data from a file or a binary stream, seekg() can help put the get pointer precisely where a particular data structure or element is located in the binary file.
Random Access:
The get pointer can be moved to a specific location inside the input stream using seekg(). You can read data from arbitrary points in the stream due to the ability to enable random access to data. For instance, you can use seekg() to skip to a particular record or data block in a big file with structured data instead of reading it all one by one.
Data recovery:
Suppose you want to detect specific patterns or markers in the data and recover the required information. In that case, you may utilize seekg() in conjunction with your data recovery techniques when you need to recover data from a corrupted file or binary data.
Configurable File Parsing:
The seekg() function lets you skip over pointless sections of the file and immediately reach the sections you require for processing when parsing configuration files or logs that follow a specified format.
Parsing:
You may want to bypass header information or directly access specific fields when processing structured data in a file, such as one that is in the CSV (Comma-Separated Values) or JSON format. You can start reading data from the specified section by positioning the get pointer with seekg() at the proper spot in the file.
Implementing Data Structures:
Sometimes, you might store serialized objects or data structures in a file. You can access and read different file sections using seekg(), which enables you to recreate the original data structures.
It's crucial to remember that seekg() can be used with different input streams, such as istringstream, which reads from strings, even though it's generally used with file input streams (ifstream). However, since some input streams, like standard input (cin), don't have a fixed position or support random access, seekg() might not be supported or might not be as helpful for them.