seekg() Function C++
seekg() in C++ is a function that helps to get an arbitrary file position in the iostream library. It is present in the fstream header file and is also a part of file handling, used to extract from the input stream by placing the pointer to the next character that is desired.
Syntax of C++ seekg() Function
There are two syntaxes of the seekg() function that can be used:
Parameters of Seekg() in C++
Position
It is of type streampos, which represents an int value, used to set the pointer to a desired position in the stream buffer. streampos is an instance of fpos class used to represent positions in narrow-oriented streams. Objects of this class support construction and conversion from int and allow consistent conversions to/from streamoff values.
dir
It is the direction that values ios_base::beg, which points to the beginning of the file, ios_base::cur, which points where the pointer currently is, and ios_base::end, which points to the end of the file.
offset
It is of type streamoff, which is used relative to dir. With respect to dir, the pointer goes further according to the value of offset given.
Return Value of C++ seekg() Function
The seekg function in C++ language sets the pointer to the desired location in the file. It returns the istream object (\*this) and changes the pointer in the file.
Exceptions of Seekg() in C++
Basic guarantee: The object is valid if an exception is thrown. If the error state flag does not show the outcome a good bit, then it will throw an exception of member type failure, and for that state, member exceptions were set.
The function catches and handles any exception, setting a bad bit thrown by an internal operation. If a bad bit was set on the last call to exceptions, the caught exception gets rethrow by the function.
Errors Faced
If the pointer points to the end of the file, it will not be reset, and the seekg function will throw errors. To prevent this, use the clear() method to reset the pointer.
Data Races
It Alters the stream object. Data races may occur because of concurrent use of the same stream object. Data Race is a condition in which a common stream object is accessed simultaneously at multiple places.
How does the Seekg() in C++ Function Work?
Following are the two ways of using the C++ seekg() method:
1. istream&seekg(streampos position)
The position is the parameter, which is the place where the pointer is changed to.
Algorithm: First of all, open a new file for I/O operations. For example, the name of the file will be “myfile.txt”. Let the current position of the file be 2. Add some characters to the file, for example, “Hello World”. Now, seek 3 characters from where the pointer is placed, that is, to 5.
Now, read the content into the buffer, output it from the file, and close it.
2. istream&seekg(streamoff offset, ios_base::seekdir dir)
offset is the value that a pointer skips and goes ahead. dir is the direction from where the pointer starts.
Algorithm: First of all, open a new file for i/o operations. For example, let the file name be “myfile2.txt” this time. Again add some characters to the file. For example, this time, let it be “Hello World”. Now seek 3 characters from the beginning, that is to 3.
Now read the content into the buffer, output it from the file, and close it.
Examples of Seekg() in C++ Function
Example of seekg using offset In the following code, we attempt to:
- Read a file from the filesystem.
- Add some text to it.
- Seek the first 4 characters from the file.
- Copy the next 5 characters from the file's contents in a buffer.
- Print the buffer data.
Input:
Output: (Assuming the file read is initially empty)
Example clearing the fail bit In the code that follows, we do the following:
- Read a file by creating an input file stream object.
- Check the end of the file status.
- Read the file contents until all the content is read and the end of the file is reached.
- Again, check the end of the file status.
- Clear the internet error state bits in the object.
- Seek the input file stream object to its beginning.
- Again, check the end of file status of the input file stream object.
Conclusion
- In C++ Programming language, seekg() is a function that helps get an arbitrary file position in the iostream library.
- There are two syntaxes of the seekg() function that can be used: istream&seekg(streampos position) & istream&seekg(streamoff offset, ios_base::seekdir dir).
- The seekg function in C++ returns The istream object (*this) and changes the pointer in the file.
- If the pointer points to the end-of-file, it will not get reset, and the seekg function will throw errors. So, to prevent this, use the clear() method to reset the pointer.
- Data races may occur concurrently using the same stream object.