You must transfer the data into an array (or vector) that you can use to manipulate the data. Then, you save the data back to the file, overwriting it. The rdbuf is really not meant for you to tamper with, and you certainly cannot use it to modify the content of the file. Not to mention that manipulating rdbuf would be very hard since it has basically not useful interface for that, e.g., you won't be able to insert, overwrite, or delete data at random places.

You can use the seekg / tellg facilities to move to certain reading position with the fstream which will allow you to read only the part of the file that is interesting, instead of loading the entire thing in memory. But that is about as far as it goes with manipulating the data using the fstream object. To be able to do more useful (and non-sequential) manipulations, you will need a more powerful random-access container, like std::vector or std::deque.