In our last article, we have explained how to use the a very important file output/write function - put() function, used to write a character to the file. In this tutorial, we are going to explain how modify the content of an existing file, but before we do that, it is critical to understand the pointer associated with each file, which allow to read the content of a file on disk. This pointer is called get pointer.
The get pointer allows us to read the content of a file when we open the file in read-only mode.
It automatically points at the beginning of file, allowing us to read the file from the beginning.
Before we show you how to access and manipulate with the get pointer to read the content of a file on the disk, we must show you how to open the file. For this, the C++ language provides us stream classes used to perform file input/read operation.
This class is used to create an input stream and gives access to get pointer, to perform the file read operation.
This class is used to used to create an input-output stream and gives access to both get and put pointers, to perform binary file read/write operations.
These file stream classes provides us a function named open(), using which we could provide location of the file stored on the disk to depending on the mode in which we open this file.
file-stream-object, is object of a file stream class used to perform a file output/write operation.
filename, is the name of file on which we are going to perform file operations.
mode, is single or multiple file modes in which we are going to open a file.
Let's see the file mode that we require to use, in order to use the put() function to perform input/read operation on a file.
Let's look at the combination of modes required to read the content of a file while working with the get pointer.
Searches for the file and opens it in read mode only and gives access to get pointer, to read the content of a file.
Access the current location of the get pointer.
Allow us to set the get pointer at the particular location in a file.
Gives us the current location of the get pointer. When the file is opened in a read-only mode, tellg() returns zero i.e. the beginning of the file.
This function is used to set the location of the get pointer to a desired position/offset. The position variable is the new position in the file i.e. an integer value representing the number of bytes from the beginning of the file.
ios::beg - offset from the beginning of the file.
ios::cur - offset from the current position in the file.
ios::end - offset from the end of the file.
Now let's try to read(twice) the content of this file by working with the get pointer and seekg() function. This file will be searched in the current directory(the same directory where we are going to store the upcoming C++ program.).
As you can see in the output, the program has twice read the same file by using the seekg() function to manipulate with the get pointer.
By setting the first parameter of seekg() function i.e. offset position to 0.
And by setting the second parameter seekg() function i.e. flag to "ios::beg".
Once the reading of content of a file completes, the EOF(End of file) is reached and the EOF flag is set on which does not allow us to access of file again for reading it again. Hence, a call to clear() function is very critically important because it sets the EOF flag off, which allows us to re-read the content of file.
In the next article, we are going to show you how to work and manipulate with the put pointer to write the content of a file on disk. So, let's turn the page then!