
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Set position with seekg() in C++ language file handling
seekg() is a function in the iostream library that allows us to seek an arbitrary position in a file. It is mainly used to set the position of the next character to be extracted from the input stream from a given file in C++ file handling.
Syntax
istream&seekg(streamoff offset, ios_base::seekdir dir); istream&seekg(streampos position); Where, position: It is the new position in the stream buffer. offset: It is an integer value of type streamoff which represents the offset in the stream’s buffer, relative to the dir parameter. dir: It is the seeking direction. It is an object of type ios_base::seekdir that can take any offset constant value. Three direction used for offset values are: ios_base::beg = from the beginning of the stream’s buffer. ios_base::cur = from the current position in the stream’s buffer. ios_base::end = from the end of the stream’s buffer.
Required steps and operations
Begin Open a data file for input/output operations. Add ‘TutorialsPoint’ to the file. Seek to 9 position from beginning of the file. Read the next 5 characters from the file into buffer F. End F with null character as terminator character. Print the contents. Close the file. End
Example
#include <fstream> #include <iostream> using namespace std; int main (int argc, char** argv) { fstream File("d.txt", ios::in | ios::out ); File << "TutorialsPoint"; File.seekg(9, ios::beg); char F[9]; File.read(F, 5); F[5] = 0; cout <<F<< endl; File.close(); }
Output
Point
- Related Articles
- tellp() in file handling with C++
- File Handling in C#
- Basics of File Handling in C
- How to set read and write position in a file in Python?
- File Handling through C++ Classes
- Basics of File Handling in C Programming
- What are the error handling techniques in C language?
- How to configure handling and formatting of log file in Selenium with python?
- File Handling in Java using FileReader and FileWriter
- Set the Background Image Position with CSS
- MySQL Command-Line Options that Affect Option-File Handling
- Handling Alerts with Cypress
- Handling Frames with Cypress
- Set file attributes in Java
- How to set the top position of an element with JavaScript?

Advertisements