seekp() function in File Handling in C++

Hey guys,
In today’s tutorial, we’ll be understanding what the seekp() function does in C++.

seekp() function in C++

So as I explained in my last tutorial,
We have a function called seekp which gets imported along with the other basic file handling operation in “fstream”.

So what does seekp() do?
The seekp() function moves the pointer to the desired location.

When we create a text file or open a text file, our pointer is set to 0. So if we start writing in that text file, we overwrite all previously written data.

Using seekp() in C++ we can navigate the pointer to the desired location and write from thereon.

It takes 1 argument i.e. position you want to set the pointer to in the text file.
NOTE: pointer starts from 0 in the text file.

Syntax:

The syntax of seekp() function is given below:
<filehandling_obj>.seekp(<position_of_pointer>);

Example:
obj.seekp(5);

Here,
obj is our file handling object. On the other hand, 5 is the position we would like to set our pointer to.

C++ Code example:

Take a look at the following example of using seekp() function:

#include <iostream>
#include <fstream>
using namespace std;
int main () 
{
  fstream obj;
  obj.open ("test.txt", ios::out);
  obj<<"Hello  World";
  int pos = 6;
  obj.seekp(pos-1);
  obj<<"...And here the text changed";
  obj.close();
  return 0;
}

Let us see the code line by line.

  1. fstream obj;
    Here we are using the function fstream that enables read and write.
    obj is the object we are using to refer to fstream.
  2. obj.open (“test.txt”, ios::out);
    Here we are opening a text file called test with basic function to write hence the “ios::out”
  3. obj<<“Hello World”;}
    Here we are writing Hello World into the test.
    If you write only these 3 lines of codes along with the headers, test would have “Hello World” Printed in it.

    Hello World
  4. int pos = 6;
    Here we are defining a variable named pos of type int and storing the value 6 in it.
    Why 6?
    Because we would like to start writing from the 6th position.
    That turns out to be “W” in Hello World.
  5. obj.seekp(pos-1);
    Using the syntax, we wrote this line of code.
    Now we are moving to the sixth-1 position i.e. 5 in our text file.
    That turns out to be the space in Hello World.
  6. obj<<“…And here the text changed”;
    And now we are writing the following text into the text file.
    Since our pointer was moved to 5, we overwrite the space and “World”.
    Hence our output becomes:

    Hello...And here the text changed
  7. obj.close();
    Here we are closing the test file we opened.

 

This function is useful especially when we know the position where we want to start overwriting from.
And hence we have covered seekp() function in C++

I hope you understood the logic and were able to execute it by yourself. If you have any doubts regarding this, feel free to ask it in the comment section. Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *