seekg() Function C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

seekg() in C++ is a function that helps to get an arbitrary file position in the iostream library. It is present in the fstream header file and is also a part of file handling, used to extract from the input stream by placing the pointer to the next character that is desired.

Syntax of C++ seekg() Function

There are two syntaxes of the seekg() function that can be used:

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Parameters of Seekg() in C++

Position

It is of type streampos, which represents an int value, used to set the pointer to a desired position in the stream buffer. streampos is an instance of fpos class used to represent positions in narrow-oriented streams. Objects of this class support construction and conversion from int and allow consistent conversions to/from streamoff values.

dir

It is the direction that values ios_base::beg, which points to the beginning of the file, ios_base::cur, which points where the pointer currently is, and ios_base::end, which points to the end of the file.

offset

It is of type streamoff, which is used relative to dir. With respect to dir, the pointer goes further according to the value of offset given.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Return Value of C++ seekg() Function

The seekg function in C++ language sets the pointer to the desired location in the file. It returns the istream object (\*this) and changes the pointer in the file.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Exceptions of Seekg() in C++

Basic guarantee: The object is valid if an exception is thrown. If the error state flag does not show the outcome a good bit, then it will throw an exception of member type failure, and for that state, member exceptions were set.

The function catches and handles any exception, setting a bad bit thrown by an internal operation. If a bad bit was set on the last call to exceptions, the caught exception gets rethrow by the function.

Errors Faced

If the pointer points to the end of the file, it will not be reset, and the seekg function will throw errors. To prevent this, use the clear() method to reset the pointer.

Data Races

It Alters the stream object. Data races may occur because of concurrent use of the same stream object. Data Race is a condition in which a common stream object is accessed simultaneously at multiple places.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

How does the Seekg() in C++ Function Work?

Following are the two ways of using the C++ seekg() method:

1. istream&seekg(streampos position)

The position is the parameter, which is the place where the pointer is changed to.

Algorithm: First of all, open a new file for I/O operations. For example, the name of the file will be “myfile.txt”. Let the current position of the file be 2. Add some characters to the file, for example, “Hello World”. Now, seek 3 characters from where the pointer is placed, that is, to 5.

Now, read the content into the buffer, output it from the file, and close it.

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

offset is the value that a pointer skips and goes ahead. dir is the direction from where the pointer starts.

Algorithm: First of all, open a new file for i/o operations. For example, let the file name be “myfile2.txt” this time. Again add some characters to the file. For example, this time, let it be “Hello World”. Now seek 3 characters from the beginning, that is to 3.

Now read the content into the buffer, output it from the file, and close it.

Examples of Seekg() in C++ Function

Example of seekg using offset In the following code, we attempt to:

  1. Read a file from the filesystem.
  2. Add some text to it.
  3. Seek the first 4 characters from the file.
  4. Copy the next 5 characters from the file's contents in a buffer.
  5. Print the buffer data.

Input:

Output: (Assuming the file read is initially empty)

Example clearing the fail bit In the code that follows, we do the following:

  1. Read a file by creating an input file stream object.
  2. Check the end of the file status.
  3. Read the file contents until all the content is read and the end of the file is reached.
  4. Again, check the end of the file status.
  5. Clear the internet error state bits in the object.
  6. Seek the input file stream object to its beginning.
  7. Again, check the end of file status of the input file stream object.

Conclusion

  • In C++ Programming language, seekg() is a function that helps get an arbitrary file position in the iostream library.
  • There are two syntaxes of the seekg() function that can be used: istream&seekg(streampos position) & istream&seekg(streamoff offset, ios_base::seekdir dir).
  • The seekg function in C++ returns The istream object (*this) and changes the pointer in the file.
  • If the pointer points to the end-of-file, it will not get reset, and the seekg function will throw errors. So, to prevent this, use the clear() method to reset the pointer.
  • Data races may occur concurrently using the same stream object.