Are IO streams portable

I was wondering as to whether IO streams are really portable(I think they should be but want be sure) ? especially when handling files used in both unix and windows.

Specifically if I dump a double value into a file in windows and then read the same value on unix from the file using iostreams, will it not cause problems because of variation in the endianness of the two operating systems ?

what about formatted io ? I read somewhere, not sure where, that when it comes to manipulating formatted data, there is some difference in the way windows and unix handle the same. Can anyone please throw some light on this issue ?


The C++ standard is abstract and does not mention any particular OS (AFAIK). So, the iostream library is portable. Having said that, the C++ standard allows for certain implementation details, including some visible to the user (and not just hidden in the standard C++ library) to be implementation dependent. This allows the provider of the OS and/or compiler to choose their preferred way to implement the C++ standard.

Any code which relies on implementation-dependent behaviour is obviously not portable, not even within different versions of the same OS and/or compiler. In other words, such code should be avoided.

Note: sometimes it is necessary to write implementation-dependent code. In this case, it is essential to guard the code against breaking of the assumed implementation-dependent behaviour. For instance, a program may assume that sizeof(long unsigned)==8 . In this case, at least a static_assert(sizeof(long unsigned)==8,"long unsigned not 8 bytes") should stop the compilation of invalid code, but ideally an alternative version should exist and be picked instead via template magic (or C-style macros). The C++11 standard actually provides the type std::uint64_t with guaranteed sizeof(std::uint64_t)==8 (and similar) to avoid such issues. However, sizeof(ostream) remains implementation dependent.


It's portable to the extent that if you write a particular sequence of bytes from one system, transfer that sequence to another system (by file or by socket, it doesn't really matter), you can read back the same sequence of bytes on the other side. If you write or read in text mode rather than binary mode, then line-endings (and I think in theory other implementation-defined special characters) might be re-interpreted.

What isn't portable is the precise representation of double as a sequence of bytes, either binary or formatted. Binary because of for example endian-ness, and formatted because of subtle differences in the runtime libraries that might mean your results can be a ulp (or sometimes more) different when encoding to string by one system and back to double by another, even if you output what should be enough decimal digits of precision. The string representation of NaN values is also left to the implementation.

As far as the C++ standard is concerned, double isn't required to be an IEEE double precision value. So there could in theory be nothing in common between the binary representations, and also big differences in terms of which values can be represented and hence which strings are output/accepted when doing formatted I/O. In practice, IEEE is commonly used and strings usually don't introduce any more error than you'd expect in floating-point operations.


File input output : Yes they are compatable. A bit stream in Unix is same as a bit stream on windows. A double value in Unix is same as that of Windows as both follow the standards.

Formatting: I am not aware of any binary formatting (in the example of double) and you may be talking about text. Text formatting in windows is different from Unix and this is the cause of many problems.

Binary Interface / Code portability : If you are using functions for IO, then it is not portable. std::input/output streams, ie, f/o/stream implementation is not portable and neither was it designed to be portable.

链接地址: http://www.djcxy.com/p/68190.html

上一篇: JavaFX 2.0 FXML子窗口

下一篇: IO流是否便携?