Difference Between Seekg() and Seekp() - Brain Mentors Skip to content

Difference Between seekg() and seekp() in C++ File-Handling – Brain Mentors

seekg() and seekp() both are functions of File Handling in C++ and they are very important and useful feature of File Handling in C++.

In File Handling of C++, we have two pointers one is get pointer and second is put pointer. They both are used to getting/reading the data from a file and putting/writing the data into a file at particularposition/location.

How to use Seekg ()

seekg() function is used to move/sets the get pointer at the desired/particular position to getting/reading data from a file (External file).

If seekg() function is used to move/sets the get pointer at the particular location. So then by using tellg() function we easily get the current position of the get pointer from the external file.

Syntax: seekg(offset, direction/seekdir);

Using this function the stream pointer is changed the position/location i.e. counting pointer position from the beginning of the file.

Stream means the flow of data that acts as an interface between files and programs. Here, stream is input stream that is coming from the ifstream class.

In this position/location of the get pointer is set to an offset value that is relative to some specific point that determined by the direction of the parameter. offsetvalue is an integer type. And direction of get pointer is of type seekdir, which determines the pointer position from where offset value is counted from.

After moving the position of the get pointer, read() function is used to getting/reading the data from an external file.

Offset/Seekdirvalues are:

  1. ios::beg: beginning of a file
  2. ios::end: end of a file
  3. ios::cur: current position of a file

Example: How to read data or set pointer position using seekg () in C++?

CODE:

Seekg() code example

OUTPUT:

Seekg() code output

Test.txt:

Seekg ()

How to use Seekp()

Whereasseekp() function is used to move/sets the put pointer at the particular/desired location to putting the data into a file (External file).

And If seekp() function is used to move the put pointer at the particular/desired location. So then by using tellp() function we easily get the current position of the put pointer from the file.

Syntax: seekp (offset, direction/seekdir);

Using this function the stream pointer is changed the position/location i.e. counting pointer position from the beginning of the file.

Stream means the flow of data that acts as an interface between files and programs. Here, stream is output stream that is coming from the ofstream class.

Same as seekg() function, here alsoposition/location of the put pointer is set to an offset value that is relative to some specific point that determined by the direction of the parameter. offsetvalue is an integer type. And direction of put pointer is of type seekdir, which determines the pointer position from where offset value is counted from.

After moving the position of the put pointer, write() function is used to putting/writing the data into an external file.

Offset/Seekdir values are:

  1. ios::beg: beginning of a file
  2. ios::end: end of a file
  3. ios::cur: current position of a file

Example: How to writedata into a file using seekp () in C++?

CODE:

Seekp() example

OUTPUT:

Seekp() output

Test.txt

Seekp() text

Sign Up and Start Learning