Seekg
In the C++ programming language, seekg() is a function in the header of the C++ Standard Library for seeking to an arbitrary position in a file.[src] This function is defined for std::ifstream class, for std::ofstream class there is a similar function seekp() (this is to avoid conflicts in case of classes that derive both std::istream and std::ostream , such as std::iostream ).[src] namespace std { istream& seekg(streampos position); istream& seekg(streamoff offset, ios_base::seekdir dir); } * position is the new position in the stream buffer.[src] This parameter is an object of type std::streampos .[src] * offset is an integer value of type std::streamoff representing the offset in the stream's buffer.[src] It is relative to the dir parameter.[src] dir is the seeking direction.[src] It is an object of type std::ios_base::seekdir that can take any of the following constant values: # std::ios_base::beg (offset from the beginning of the stream's buffer).[src] # std::ios_base::cur (offset from the current position in the stream's buffer).[src] # std::ios_base::end (offset from the end of the stream's buffer).[src] Note: If the end of file on the stream has previously been reached, seekg will not reset it but will return an error in many implementations.[src] To clear the end of the file bit first, use the clear() method.…[src]
In the C++ programming language, seekg() is a function in the header of the C++ Standard Library for seeking to an arbitrary position in a file.[src] This function is defined for std::ifstream class, for std::ofstream class there is a similar function seekp() (this is to avoid conflicts in case of classes that derive both std::istream and std::ostream , such as std::iostream ).[src] namespace std { istream& seekg(streampos position); istream& seekg(streamoff offset, ios_base::seekdir dir); } position is the new position in the stream buffer.[src] This parameter is an object of type std::streampos .[src] offset is an integer value of type std::streamoff representing the offset in the stream's buffer.[src] It is relative to the dir parameter.[src] dir is the seeking direction.[src] It is an object of type std::ios_base::seekdir that can take any of the following constant values: std::ios_base::beg (offset from the beginning of the stream's buffer).[src] std::ios_base::cur (offset from the current position in the stream's buffer).[src] std::ios_base::end (offset from the end of the stream's buffer).[src] Note: If the end of file on the stream has previously been reached, seekg will not reset it but will return an error in many implementations.[src] To clear the end of the file bit first, use the clear() method.[src] This is a relatively common mistake and if seekg() is not performing as expected, it is wise to clear the fail bit, as shown below.[src] ==Example of seekg== import std; using std::fstream; using std::ios; int main() { // Open a new file for input/output operations discarding any current // content in the file (assumes a length of zero on opening) fstream myFile("test.txt", ios::in | ios::out | ios::trunc); // Add the characters "Hello World" to the file myFile ==Example clearing the fail bit== import std; using std::ifstream; using std::ios; using std::string; int main() { string line; // Creates a new ifstream object and associates it with a file passed in via the parameters.[src] // The object is then checked to see if the end-of-file has been reached, if the file has data // then this will be false.[src] ifstream myFile(argv[1], ios::in); std::println(myFile.eof()); // Continuously loops over each line of the file until the end of the file while (!myFile.eof()) { std::getline(myFile, line); } // Again outputs the end-of-file status for the stream, this time the answer will be true std::println(myFile.eof()); // Seeks to the very beginning of the file, clearing any fail bits first (such as the end-of-file bit) myFile.clear(); myFile.seekg(0, ios::beg); // Outputs the status of the stream again, the result will be false std::println(myFile.eof()); myFile.close(); } ==References==[src]
28 sentences · 28 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Seekg." encyclopegia.org, 2025, https://encyclopegia.org/wiki/seekg. Accessed 30 November 2026.
Chicago
"Seekg." encyclopegia.org. Accessed November 30, 2026. https://encyclopegia.org/wiki/seekg.
APA
encyclopegia.org. (2025). Seekg. Retrieved November 30, 2026, from https://encyclopegia.org/wiki/seekg
Accessed November 30, 2026 · CC BY-SA 4.0 · citation page · JSON