I have a generic function:
bool WriteFile(const string& strFileContent, const wpath& pathFile)
In this function I have the following line:
std::ofstream outStream(pathFile.string().c_str(), std::ios::out);
Since this function is generic, I sometimes use it to write binary files. I noticed that using openmode std::ios::out is not the right one for binary data and the written files are not as I expected, meaning, files are not equal to the actual data defined in strFileContent.
So I've made this simple fix:
std::ofstream outStream(pathFile.string().c_str(), std::ios::out | std::ios::binary);
Which resolved the issue for binary files and also worked perfect for text files as well.
Question
if open mode std::ios::out | std::ios::binary works for both binary and text data, isn't one of them redundant?
If so, should I always use std::ios::out | std::ios::binary?
If not, In which cases this using std::ios::out | std::ios::binary is not recommended?
I've read some related posts and can't find much new stuff.
Except the newline conversion, I can't find any other reasons to prefer text mode.
By the way, in the GNU C Library, and on all POSIX systems, there is no difference between text streams and binary streams.
see here for how this is explained in GCC documentation.
I would really want to find some examples about the emphasised text.