I am trying to open a file for output and add to it. After adding to it, I want to transfer my output position to another location in the file and overwrite the existing data. As far as I understand, std::ios_base::app will be force , all entries will be at the end of the file, which I do not want to do. As such, I believe that std::ios_base::ate is the correct flag to go to std::ofstream::open() . However, it does not seem to work as expected:
// g++ test.cpp // clang++ test.cpp // with and without -std=c++11
In particular, it seems that std::ios_base::ate does not move the initial output pointer to the end with the example described above. Obviously, this will cause the first record to be overwritten at the beginning of the file (which caused my problem).
It seems that the implementation is incorrect, or cplusplus.com is incorrect ("The output position starts at the end of the file.") And cppreference.com is ambiguous ("look for the end of the stream immediately after opening": which stream?).
There is obviously an easy way: just use stream.seekp(0, std::ios_base::end) .
So my question is: is my code wrong? Improper execution? Are link sites listed? Any insight would be appreciated.
c ++ c ++ 11 fstream
inetknght
source share