The “Seekp” Function:
The “seekp” stands for seek put. This function is used to seek & put the file pointer position (the byte number of next byte) from where the data in the file is to be updated.
The general form of this function is:
Where
object: represents the created object of ofstream (or fstream).
seekp: represents the member function of the object.
pos: represents position (the byte number of next byte) where the data is to he written or over written. It is a long integer type parameter.
For example, to position the file pointer to the position number 10 (10th byte) in the file to write data, the function is written as:
If a file has a group of records and each record has a length of 50 bytes then the size of record is multiplied with 10 to put the file pointer to the position of specified record, e.g.
The file position pointer can also be set to update the data into the file relative to the beginning of file, end of the file or from the current file pointer position of the file. In this method, the syntax “seekp” function is:
object.seekp (pos, rp);
Where
pos: represents position (the byte number of next byte) where the data is to he written or over written. It is a long integer type parameter.
rp: represents the relative position.
For example, to put the file pointer position 10 bytes (for writing data) relative to the beginning of file, the function is written as:
For example, to seek the tile pointer position 10 bytes (for writing data) relative to the current position of file pointer in the file, the function is written as:
For example, to put the file pointer position 10 bytes (for writing data) back relative to the end of file, the function is written as:
object.seekp(-10, ios : : end);
comment closed