File I/O
File Positioning
Navigate streams using seek and tell operations on get and put pointers.
Interview: Navigating get and put pointers, clearing error states before seeking, and calculating file sizes efficiently.
C++ streams maintain internal markers to track the current read and write positions. Using seek and tell operations, you can jump to specific bytes inside a file to implement random access.
Get and Put Pointers
Input streams maintain a Get pointer (tellg/seekg). Output streams maintain a Put pointer (tellp/seekp).
Tell Operations
tellg() and tellp() return the current position (in bytes) of the read/write pointer as a std::streampos.
Seek Directions
Seek relative to the beginning (ios::beg), current position (ios::cur), or end (ios::end).
Clearing State Flags
If you read to the end of a file, the stream sets the eof and fail flags. If these flags are active, subsequent seek operations will fail silently. You must call stream.clear() to reset the flags before calling seekg() or seekp().
Code Walkthrough
Calculates file size in bytes and reads data from a specific offset.
#include <iostream> #include <fstream>std::streamsize getFileSize(const std::string& filepath) { std::ifstream file(filepath, std::ios::binary); if (!file) return 0;
// Seek to the end of file file.seekg(0, std::ios::end); // Tell position (returns bytes from beginning) std::streamsize size = file.tellg(); return size; }
void readFromOffset(const std::string& filepath, std::streamoff offset) { std::ifstream file(filepath, std::ios::binary); if (!file) return;
// Seek to specific offset from the beginning file.seekg(offset, std::ios::beg);
char buffer[10]; if (file.read(buffer, sizeof(buffer))) { std::cout << "Read 10 bytes from offset " << offset << std::endl; } }
int main() { std::cout << "File Size: " << getFileSize("data.bin") << " bytes\n"; readFromOffset("data.bin", 4); return 0; }
Interview-Relevant Information
Q: What is the difference between seekg and seekp?
Answer: seekg (seek get) moves the read position pointer of input streams (derived from std::istream). seekp (seek put) moves the write position pointer of output streams (derived from std::ostream). On basic std::fstream objects, they can point to different positions.
Q: Why do seek operations fail after reading past the end of a file?
Answer: Reading past the end of a file sets the stream's eofbit and failbit. When these error states are active, the stream ignores further operations, including seek. Calling stream.clear() resets these flags and allows you to seek back.
Quick Checklist
Did you clear the stream states before seeking? Did you use the correct suffix (g for read, p for write)? If yes, your stream navigation is correct.
Use Cases
Querying the file size of binary assets dynamically before loading them into memory.
Performing raw updates inside custom binary files or page tables without reloading the entire file.
Common Mistakes
Confusing seekg and seekp on single fstream objects, resulting in incorrect read or write positions.
Attempting seek operations after reaching the end of the file without calling clear() first.