ADVERTISEMENT
ADVERTISEMENT

Home » C++ programs » C++ file handling programs

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++ program to demonstrate the example of tellg(), seekg() and seekp()

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

Program:

#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
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.