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.
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.
Exceptions: 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
size of record: 24 record no 1 (position int file 0) rno: 1 name: geek