So, if I'm understanding you correctly, you're trying to do something like this:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
void write(const char *filename, const char *message) {
std::ofstream out(filename);
out << message;
out.close();
}
void modify(const char *filename, int pos, const char *change) {
std::ofstream file(filename, std::ios::in | std::ios::out);
file.seekp(pos);
file << change;
file.close();
}
void dump(const char *filename) {
std::ifstream file(filename);
std::string line;
while(std::getline(file, line)) {
std::cout << line << std::endl;
}
file.close();
}
int main() {
const char *filename = "rewritetest.txt";
const char *message = "Hello, World! Nice to meet you.\n";
write(filename, message);
modify(filename, 7, "Steve");
dump(filename);
std::remove(filename);
std::cin.get();
return 0;
}
That program outputs
Code:
Hello, Steve! Nice to meet you.
Of course, I don't seek to the end of the file to determine its size, nor do I use binary mode, but it seems to indicate that what you are attempting is possible.
I suggest trying different combinations of flags (I'm sure you're already doing that!) and simple programs like the one above to figure out what is happening.