Paul
2007-10-29 13:09:01 UTC
I am trying to write code that would open a file, seek to the end, and
replace the last character in the file. The problem is that only
std::ios_base::app supplied in the mode argument appears to be able to
preserve the file contents, all others truncating it, and std::ios_base::app
assumes that writing will start at the end wherever you seek to after opening
the file.
This is what I was trying to make work (I know at this point that the file
exists and is not empty):
std::ofstream f("ABI_TEST.CSV", std::ios_base::ate);
f.seekp(-1, std::ios_base::end);
f << '\0';
I tried to use std::fstream supplying 'std::ios_base::out |
std::ios_base::ate' for the mode but only with std::ios_base::app will the
file not be truncated. As far as I know, std::ios_base::trunc should not be
applied by default and std::ios_base::ate should work for files, too.
Thank you.
replace the last character in the file. The problem is that only
std::ios_base::app supplied in the mode argument appears to be able to
preserve the file contents, all others truncating it, and std::ios_base::app
assumes that writing will start at the end wherever you seek to after opening
the file.
This is what I was trying to make work (I know at this point that the file
exists and is not empty):
std::ofstream f("ABI_TEST.CSV", std::ios_base::ate);
f.seekp(-1, std::ios_base::end);
f << '\0';
I tried to use std::fstream supplying 'std::ios_base::out |
std::ios_base::ate' for the mode but only with std::ios_base::app will the
file not be truncated. As far as I know, std::ios_base::trunc should not be
applied by default and std::ios_base::ate should work for files, too.
Thank you.