Is the default mode of ofstream implementation defined?

北城以北 提交于 2019-12-01 11:37:28

问题


Given the following code:

std::ofstream stream("somefile");

if (!stream)
{
   return 1;
}

When invoking .write(....) and using stdc++ and libc++ the stream is in binary mode (std::ios::binary).

However when using MSVC (2015/2017RC1) it seems to be in text mode or something weird, because the the resulting file is larger than what is actually written.

But if i explicitly set the mode std::ios::binary MSVC behaves similarly to the std::ofstream implementations of other standard libraries mentioned earlier.


Example code:

#include <vector>
#include <cstdio>
#include <fstream>

std::size_t fsz(const char* filename) {
    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
    return static_cast<std::size_t>(in.tellg());
}

int main() {
   std::ofstream stream("filename");

   if (!stream)
      return 1;

   std::vector<unsigned long long int> v = {0x6F1DA2C6AC0E0EA6, 0x42928C47B18C31A2, 0x95E20A7699DC156A, 0x19F9C94F27FFDBD0};

   stream.write(reinterpret_cast<const char*>(v.data()),v.size() * sizeof(unsigned long long int));

   stream.close();

   printf("expect: %d\n", v.size() * sizeof(unsigned long long int));
   printf("file size: %d\n", fsz("filename"));

   return 0;
}

Output for the above code when run with msvc:

expect: 32 
file size: 33

Output for the above code when run with libc++, stdc++:

expect: 32 
file size: 32

The difference can get much larger, it depends on how much data is written and the contents of the data.

at the end my question is still the same, is it undefined or unspecified behavior?


changing the above vector to the following makes the example more obvious as to whats going on.

std::vector<unsigned long long int> v = {0x0A0A0A0A0A0A0A0A, 0x0A0A0A0A0A0A0A0A, 0x0A0A0A0A0A0A0A0A, 0x0A0A0A0A0A0A0A0A};

回答1:


The default mode used by the stream constructor is ios_base::out. As there is no explicit text mode flag, this implies the stream is opened in text mode. Text mode only has an effect on Windows systems, where it converts \n characters to CR/LF pairs. On POSIX systems it has no effect, and text and binary modes are synonymous on these systems.




回答2:


When I run your code on windows using g++ and libstdc++, i get the following result:

expect: 32
file size: 33

So the problem is not compiler specific, but rather OS specific.

While C++ uses a single character \n to represent a line ending in a string, Windows uses two bytes 0x0D and 0x0A for a line ending in a file. This means that if you write a string into a file in text mode, all occurrences of the single character \n are written using those two bytes. That's why you get additional bytes in the file size of your examples.



来源:https://stackoverflow.com/questions/42412759/is-the-default-mode-of-ofstream-implementation-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!