diff --git a/libs/integer/doc/binary_stream.html b/libs/integer/doc/binary_stream.html index a3563e5..c431b7b 100644 --- a/libs/integer/doc/binary_stream.html +++ b/libs/integer/doc/binary_stream.html @@ -11,14 +11,14 @@
-The C++ standard library's stream I/O facilities are type-safe and very -convenient for performing formatted (i.e. human readable) I/O, but offer only -rudimentary and not very type-safe operations for performing binary I/O. +convenient for performing formatted (i.e. human readable) I/O. But they offer only +rudimentary and not very type-safe operations for performing unformatted binary I/O. Although formatted I/O is often preferable, some applications need the speed and -storage efficiency of binary I/O or need to interoperate with third-party -applications that require binary file or network data formats.
+storage efficiency of unformatted binary I/O or need to interoperate with third-party +applications that require unformatted binary file or network data formats.Standard library streams can be opened with filemode
std::ios_base::binary
, so binary I/O is possible. But the only
unformatted I/O functions available are get()
, put()
,
@@ -33,9 +33,9 @@ have in mind is for the simple case where the application that reads data knows
the data types, so this is not as complicated as the general marshalling
situation.
This proposal provides a simple solution that works will standard library
-input and output streams. The one caveat is that they should be open with
-filemode std::ios_base::binary
.
This proposal provides a simple solution that works with standard library
+input and output streams. The one caveat is that the stream must be opened with filemode std::ios_base::binary
+to avoid certain data values being treated as line endings.
namespace boost @@ -62,19 +62,33 @@ are implementation supplied types.-int main() { - int i = 0x41424344; - std::cout << std::hex << i << " " << bin(i) << '\n'; + fstream f("binary_stream_example.dat", + std::ios_base::trunc | std::ios_base::in | std::ios_base::out | std::ios_base::binary); + + int32_t x = 0x01020304; + int32_t y = 0; + + f << bin(x); + f.seekg(0); + f >> bin(y); + + BOOST_ASSERT(x == y); + return 0; }On a little-endian machine, the output is:
+The file produced with be four bytes in length. On a big-endian machine, the +contents in hexadecimal are:
-+41424344 DCBA+01020304+On a little-endian machine, the contents in hexadecimal are:
++-04030201
Last revised: -22 April, 2011
+24 April, 2011© Copyright Beman Dawes, 2009, 2011
Distributed under the Boost Software License, Version 1.0. See www.boost.org/ LICENSE_1_0.txt
diff --git a/libs/integer/example/binary_stream_example.cpp b/libs/integer/example/binary_stream_example.cpp new file mode 100644 index 0000000..c943619 --- /dev/null +++ b/libs/integer/example/binary_stream_example.cpp @@ -0,0 +1,30 @@ +// binary_stream_example.cpp ---------------------------------------------------------// + +// Copyright Beman Dawes 2011 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +#include+#include +#include +#include + +using namespace boost; +using namespace std; + +int main() +{ + fstream f("binary_stream_example.dat", + std::ios_base::trunc | std::ios_base::in | std::ios_base::out | std::ios_base::binary); + + int32_t x = 0x01020304; + int32_t y = 0; + + f << bin(x); + f.seekg(0); + f >> bin(y); + BOOST_ASSERT(x == y); + + return 0; +} diff --git a/libs/integer/test/endian-in-sandbox/binary_stream_example/binary_stream_example.vcxproj b/libs/integer/test/endian-in-sandbox/binary_stream_example/binary_stream_example.vcxproj new file mode 100644 index 0000000..b17f084 --- /dev/null +++ b/libs/integer/test/endian-in-sandbox/binary_stream_example/binary_stream_example.vcxproj @@ -0,0 +1,84 @@ + + + \ No newline at end of file diff --git a/libs/integer/test/endian-in-sandbox/endian-in-sandbox.sln b/libs/integer/test/endian-in-sandbox/endian-in-sandbox.sln index 444543a..a4e5c89 100644 --- a/libs/integer/test/endian-in-sandbox/endian-in-sandbox.sln +++ b/libs/integer/test/endian-in-sandbox/endian-in-sandbox.sln @@ -19,6 +19,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "endian_binary_stream_test", EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "endian_flip_test", "endian_flip_test\endian_flip_test.vcxproj", "{9FA33B0B-2B00-49E8-A892-E049D86076A9}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "binary_stream_example", "binary_stream_example\binary_stream_example.vcxproj", "{06736C67-6305-4A9F-8D10-850FD0CE907D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -61,6 +63,10 @@ Global {9FA33B0B-2B00-49E8-A892-E049D86076A9}.Debug|Win32.Build.0 = Debug|Win32 {9FA33B0B-2B00-49E8-A892-E049D86076A9}.Release|Win32.ActiveCfg = Release|Win32 {9FA33B0B-2B00-49E8-A892-E049D86076A9}.Release|Win32.Build.0 = Release|Win32 + {06736C67-6305-4A9F-8D10-850FD0CE907D}.Debug|Win32.ActiveCfg = Debug|Win32 + {06736C67-6305-4A9F-8D10-850FD0CE907D}.Debug|Win32.Build.0 = Debug|Win32 + {06736C67-6305-4A9F-8D10-850FD0CE907D}.Release|Win32.ActiveCfg = Release|Win32 + {06736C67-6305-4A9F-8D10-850FD0CE907D}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE+ ++ +Debug +Win32 ++ +Release +Win32 ++ +{06736C67-6305-4A9F-8D10-850FD0CE907D} +Win32Proj +binary_stream_example ++ + +Application +true +Unicode ++ +Application +false +true +Unicode ++ + ++ ++ + + ++ + + + +true ++ +false ++ ++ + ++ +Level3 +Disabled +WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) +Console +true + ++ ++ + +Level3 ++ +MaxSpeed +true +true +WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) +Console +true +true +true + ++ ++ + + +