#includeint main() { std::ofstream file("example.txt", std::ios_base::out); file << "Hello, world!\n"; file.seekp(0, std::ios_base::beg); // move the write pointer to the beginning of the file file << "Goodbye, world!\n"; file.close(); return 0; }
#includeThis example opens a file called `example.txt` for writing and writes the string "Hello, world!" to it. Then it uses `seekp` to move the write pointer forward by 5 characters and writes the string "there" at that position. Finally, it closes the file. In both examples, the `std::ofstream::seekp` function is used to move the write pointer to a specific position in the file. The first argument specifies the offset from the specified position, and the second argument specifies the position relative to which the offset is calculated (`std::ios_base::beg` indicates the beginning of the file). The `std::ofstream::seekp` function is a part of the C++ standard library, and you can use it by including the `#include int main() { std::ofstream file("example.txt", std::ios_base::out); file << "Hello, world!\n"; file.seekp(std::streamoff(5), std::ios_base::beg); // move the write pointer forward by 5 characters file << "there"; file.close(); return 0; }