ostream::seekp(pos) method in C++ with Exmaples

The seekp(pos) method of ostream in C++ is used to set the position of the pointer in the output sequence with the specified position. This method takes the new position to be set and returns this ostream instance with the position set to the specified new position.

Syntax:

ostream& seekp(streampos pos);

Parameter: This method takes the new position to be set as the parameter.



Return Value: This method returns this ostream instance with the position set to the specified new position.

Excpetions: If the operation sets an internal state flag (except eofbit) that was registered with member exceptions, the function throws an exception of member type failure.

Below examples demonstrate the use of seekp() method in C++:

Example 1: To show the use of seekp() with Binary file

  • Take the input from the user about the serial number of the record to be displayed
  • Pass n to the function then open the file in reading mode
  • Place the write pointer at the starting of record by seekp((n-1)*Sizeof (object))
  • Write the record to the file and then close it
  • Open the file read the data and then close the file
filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to show the use of
// ostream::seekp() method using binary file
  
#include <bits/stdc++.h>
using namespace std;
  
class student {
    int rno;
    char name[20];
  
public:
    void getdata()
    {
        name = "geek" rno = 11;
    }
  
    void putdata()
    {
        cout << rno << endl
             << name << endl;
    }
  
    // accepts the serial number
    // of record to be displayed
    void DisplayRecordAtPosition(int);
};
  
void student::DisplayRecordAtPosition(int n)
{
  
    ofstream ofs;
  
    // opening the file in writing mode
    ofs.open("he.dat", ios::out | ios::binary);
  
    // displays the size of the object
    // sizeof object is 22
    // char[20]+int = 1*20 + 2 = 22
    cout << "size of record: "
         << sizeof(s) << endl;
  
    // Using seekp() method to change position
    ofs.seekp((n - 1) * sizeof(student));
  
    // Writing in the new position
  ofs.write(char*)this, sizeof(student));
  
  // Closing the output stream
  ofs.close();
  
  ifstream ifs;
  ifs.open("he.dat", ios::in | ios::binary);
  ifs.seekg((n - 1) * sizeof(student);
            ifs.read((char)*) this,
            sizeof(student)) ";
      putdata();
  ifs.close();
}
  
// Driver code
int main()
{
    student s;
    int pos = 1;
  
    cout << "record no " << pos
         << " (position int file "
         << pos - 1 << ")\n";
    s.DisplayRecordAtPosition(pos);
  
    return 0;
}

chevron_right


Output:

size of record: 22 
record no 1 (position int file 0)
rno: 1
name: vinay



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.