C++ tellg(), seekg() and seekp() Example

Here, we are going to learn how tellg(), seekg() and seekp() functions works in C++ programming language? C++ program to demonstrate example of tellg(), seekg() and seekp() functions. Submitted by IncludeHelp, on April 15, 2020

tellg(), seekg() and seekp() functions are used to set/get the position of get and put pointers in a file while reading and writing.

Syntaxes

// tellg()
streampos tellg();

// seekg()
istream& seekg (streampos pos);
istream& seekg (streamoff off, ios_base::seekdir way);

// seekp()
stream& seekp (streampos pos);
ostream& seekp (streamoff off, ios_base::seekdir way);

Here,

  • pos – represents the new absolute position within the stream (from the beginning).
  • off – represents the offset to seek.
  • pos – represents the following constants,
    • ios_base::beg / ios::beg – beginning of the stream.
    • ios_base::cur / ios::cur – current position in the stream.
    • ios_base::end / ios::end – end the stream.

C++ example of tellg(), seekg(), and seekp() functions

In the below program, we are using a file 'my.txt', the file contains the following text,

File: my.txt

IncludeHelp is specially designed to provide help to students, 
working professionals and job seekers
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    fstream F;
    // opening a file in input and output mode
    F.open("my.txt", ios::in | ios::out);
    
    // getting current location
    cout << F.tellg() << endl;
    
    // seeing 8 bytes/characters
    F.seekg(8, ios::beg);
    // now, getting the current location
    cout << F.tellg() << endl;
    // extracting one character from current location
    char c = F.get();
    // printing the character
    cout << c << endl;
    
    // after getting the character,
    // getting current location
    cout << F.tellg() << endl;
    // now, seeking 10 more bytes/characters
    F.seekg(10, ios::cur);
    // now, getting current location
    cout << F.tellg() << endl;
    // again, extracing the one character from current location
    c = F.get();
    // printing the character
    cout << c << endl;
    
    // after getting the character,
    // getting current location
    cout << F.tellg() << endl;
    // again, seeking 7 bytes/characters from beginning 
    F.seekp(7, ios::beg);
    // writting a character 'Z' at current location
    F.put('Z');
    // now, seeking back 7 bytes/characters from the end
    F.seekg(-7, ios::end);
    // now, printing the current location
    cout << "End:" << F.tellg() << endl;
    // extracting one character from current location
    c = F.get();
    // printing the character
    cout << c << endl;
    
    // closing the file
    F.close();
    return 0;
}

Output

0
8
e
9
19
i
20
End:93
s

After the program execution the file content is,

IncludeZelp is specially designed to provide help to students, 
working professionals and job seekers

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.