If your replacement text is the same, exact length as the original text, you can open the file as read/write and overwrite the text.
The traditional method (since reel-to-reel tapes), has been to copy the text that is not modified to a new file, then the modified text, then the remainder of the original text.
I recommend using std::getline
and std::string
.
If you really need performance, you may want consider double buffering.
Edit 1: example
for (unsigned int i = 0; i < 76; ++i)
{
std::string text;
std::getline(original_file, text);
new_file << text << "\n";
}
// Write new text to new file
// Read old text and ignore it.
// Copy remaining text to new file.
Background
Although files can be treated as random access (meaning you can seek to a random position), the text is not of fixed length. In general, text files can be considered as containing variable length records of text. The only way to count a line is to read until a newline is found. So in order to seek to line 76, one has to count line endings until 76 is found.