KEMBAR78
File Pointer | PDF
0% found this document useful (0 votes)
11 views2 pages

File Pointer

The document explains the functions seekg() and seekp() for moving the get and put pointers in a file, respectively. It also details the tellg() and tellp() functions for retrieving the current positions of these pointers. Additionally, it provides examples of how to use seekg() to navigate within a file using absolute and relative positions.

Uploaded by

Sadman Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

File Pointer

The document explains the functions seekg() and seekp() for moving the get and put pointers in a file, respectively. It also details the tellg() and tellp() functions for retrieving the current positions of these pointers. Additionally, it provides examples of how to use seekg() to navigate within a file using absolute and relative positions.

Uploaded by

Sadman Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

• seekg() (Get Pointer):

Used to move the get pointer, which is used for reading, to a new position within a file. It
allows you to specify the new position from a reference point (beginning, current, or end of
the file).

• seekp() (Put Pointer):

Used to move the put pointer, which is used for writing, to a new position within a file. It
works similarly to seekg() in that you can specify the new position from a reference point.

• tellg() (Get Pointer):

Returns the current position of the get pointer in a file. It provides the byte offset from the
beginning of the file where the get pointer is currently located.

• tellp() (Put Pointer):

Returns the current position of the put pointer in a file. It provides the byte offset from the
beginning of the file where the put pointer is currently located.

There are two main ways to use seekg():

• seekg(streampos position):

This sets the get pointer to an absolute position within the file, specified by
the position argument. streampos represents a position within the stream.

• seekg(streamoff offset, ios_base::seekdir dir):

This sets the get pointer to a position relative to a specific point in the file.

• offset is the number of bytes to move the pointer.

• dir specifies the starting point for the offset and can be one of the following:

• ios_base::beg: Beginning of the stream.

• ios_base::cur: Current position of the get pointer.

• ios_base::end: End of the stream.

Example:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream file("example.txt", ios::in);
if (file.is_open()) {
// Move to the 5th byte from the beginning
file.seekg(5, ios::beg);
char ch;
file.get(ch);
cout << "Character at position 5: " << ch << endl;

// Move 2 bytes forward from the current position


file.seekg(2, ios::cur);
file.get(ch);
cout << "Character after moving 2 bytes forward: " << ch << endl;

// Move 3 bytes back from the end


file.seekg(-3, ios::end);
file.get(ch);
cout << "Character 3 bytes from the end: " << ch << endl;

file.close();
} else {
cout << "Error opening file" << endl;
}
return 0;
}

You might also like