ofstream and FILE behaviour
I'm modiefing the source of an application which was originally written in C, to a more C++ (object oriented) code.
The application uses a FILE struct, and I want to use an ofstream object. But I have a question about the behaviour of FILE and ofstream.
The original C code:
Code:
// appFile is a FILE struct
// Set the pointer to the right position
fseek (appFile, patchOffset, SEEK_SET);
// Write the patch
fwrite (pAH, 1, ddmemSize, appFile);
fclose (appFile);
My first question:
What exactly does fwrite do? Just put the contents at the pointer, overwriting any data, what on that place exists, or does it append the data there, and move back any data that exists on that place?
My C++ code:
Code:
ofstream file(this -> filename.c_str(), ios::out | ios::binary | ios::app);
if(!file.is_open())
{
throw std::runtime_error("Could not open file for writing");
}
file.seekp(this -> patch_offset);
file.write(reinterpret_cast<char *>(this -> dldi_section), dldi_mem_size);
file.close();
My second question:
How exactly does ofstream write to the file?
I've experimented a bit with it, and when I don't use the ios::app flag, it looks like everything beyond the put pointer is truncated. (the patched file gets a very small filesize, which doesn't happen with the original code).
But when I do use the ios::app flag, the patched file filesize is always a few KB's bigger then when I patch it with the original app. And everytime I patch it again, it gets a few KB's bigger. So it's clear that when using the ios:app flag, all data after the put pointer is moved, and not overwritten.
So can anybody answer my questions?