Javatpoint Logo
Javatpoint Logo

seekg() function in C++

Introduction:

In this article, we will learn about the seekg() function in C++. The seekg() function enables accessing any file position in the iostream library. It is a component of file handling and can be found in the fstream header file. It is used to extract from the input stream by positioning the pointer to the desired character's subsequent position.

Syntax of seekg() function:

The seekg() method can be used with one of two syntaxes:

1st syntax:

2nd syntax:

Seekg()'s parameters in C++:

There are various parameters of seekg() function in C++. These parameters are as follows:

Position

The streampos data type is used to set the pointer to a specific location in the stream buffer by representing an int value. Positions in narrow-oriented streams are represented by an instance of the fpos class called streampos. Objects of this class enable for consistent conversions to and from streamoff values as well as creation and conversion from int.

dir

It is the direction indicated by the variables ios_base::beg, ios_base::cur, and ios_base::end, which point to the file's beginning, middle, and end.

offset

It is used to dir and is of the streamoff type. The pointer advances to dir by the value of the provided offset.

Return Value of the seekg() function

In the C++ programming language, the seekg() function sets the pointer to the requested location in the file. It modifies the pointer in the file and returns the istream object (*this).

Seekg() exceptions in C++

Basic assurance: The object is still valid if an exception is raised. A member-type failure exception will be thrown if the error state flag does not sufficiently indicate the outcome and member exceptions were set for that state.

The function catches and handles any exception, which also sets any bad bits thrown by internal operations. If a bad bit was set on the previous call to exceptions, the function will rethrow the exception that was caught.

Problems Found:

The seekg() function will fail, and the pointer will not be reset if it points to the end of the file. Use the clear() function to reset the pointer to avoid this.

Races in Data:

The stream object is altered. Data races could happen because the same stream object is being used concurrently. A data race occurs when multiple locations simultaneously access the same stream object.

How does the seekg() function operate in C++?

There are two methods for utilizing the seekg() method. These methods are as follows:

1. Istream &Seekg (streampos position):

The parameter that determines where the pointer is changed to is the position.

Algorithm:

First, open a new file for I/O operations. For instance, the file's name will be "jtp.txt". Let the file's current location be 2. Write a few characters in the file, such "welcome to JTP". From where the pointer is currently located, seek 3 characters, or to 5.

Write the file's content into the buffer, then close the buffer.

2. istream&seekg(streamoff offset, seekdirdir, ios_base::seekdir offset)

A pointer's offset is the value it skips and advances to. The pointer's initial direction is indicated by the letter dir.

Algorithm:

First, open a new file for I/O operations. For illustration, make the filename this time "jtp2.txt". Add some characters to the file once more. For instance, let it say "welcome to jtp" this time. Find the first 3 characters, i.e., to 3now.

According to the above two methods, the code will be as follows:

jtp.seekg(3) ;

jtp2.seekg(3, ios::beg) ;

Examples of Seekg() functions in C++

Let's take an example of the seekg() function with offset in the code that follows:

  • Check out a file from the filesystem.
  • Include some text there.
  • Find the file's first 4 characters.
  • The contents of the file's following five characters are copied into a buffer.
  • Print the data in the buffer.

Code:

Output:

Copied data from the file: o, th

Conclusion:

Seekg() function is a function in the C++ programming language that aids in obtaining an arbitrary file position in the iostream library. The seekg() function has two possible syntaxes: istream&seekg(streampos position) and istream&seekg(streamoff offset, ios_base::seekdirdir).

The int value is used to set the pointer to a specific location in the stream buffer that is represented by the streampos type parameter position, which is utilized in programming.

The direction is indicated by the values ios_base::beg, ios_base::cur, and ios_base::end of the parameter. The dir is the direction to the file's beginning, current location, and end.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA