Topic: Binary I/O


Author: kprateek88@yahoo.com (Prateek R Karandikar)
Date: Thu, 20 May 2004 21:11:05 +0000 (UTC)
Raw View
Why aren't there functions for binary I/O? The possible functions
could be:

template<typename Ch, typename Tr=std::char_traits<Ch> >
class std::basic_ostream : virtual public std::basic_ios<Ch,Tr>
{
 //...
 public:
 //...
 basic_ostream &write_binary(const void *buf, size_t n);
 // write n bytes (not characters) from buf.
};
//Similarly read_binary in basic_istream

This function would be independent of Ch and Tr.(Or is it a bad idea
to have member functions in a template class not related to the
template parameters?)

The usual argument given against this is portability (1's complement,
2's complement, endianness, etc). However all this comes into picture
only when we try to *interpret* the bytes in a file as integers and do
mathematical operations on them. If we just treat the file as a bit
array and do bitwise (<<, >>, &, |, ^, ~) operations on the data from
a file, and write the result to another file, bit-for-bit binary I/O
would be suitable. This would be extremely useful for file encryption
algorithms, where the entire file, or blocks of a file could be read
into memory. In the absence of binary I/O functions, we have to use
read and write which take a Ch* and const Ch* respectively. We have to
reinterpret_cast from unsigned int * or unsigned char *to Ch*, and
convert number of bytes to number of "Ch"s (which in practice is
mostly the same, however they differ conceptually). Here I wrote
unsigned int * or unsigned char * not because I am treating the data
as an array of integers or characters, but just as a sequence of bytes
(raw memory).

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: sebormartin@netscape.net (Martin Sebor)
Date: Fri, 21 May 2004 23:24:03 +0000 (UTC)
Raw View
kprateek88@yahoo.com (Prateek R Karandikar) wrote in message news:<607f883e.0405201123.718c18a6@posting.google.com>...
> Why aren't there functions for binary I/O?

stream objects use stream buffers for low-level I/O. By design, stream
buffers traffic in ordinary characters, not in raw bytes. They may
also need to do a character conversion between the internal encoding
(e.g., wchar_t) and the external representation (invariably char). So
binary I/O wouldn't even make sense.

Martin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: richard@ex-parrot.com (Richard Smith)
Date: Sat, 22 May 2004 01:00:00 +0000 (UTC)
Raw View
kprateek88@yahoo.com (Prateek R Karandikar) wrote in message news:<607f883e.0405201123.718c18a6@posting.google.com>...
> Why aren't there functions for binary I/O? The possible functions
> could be:
>
> template<typename Ch, typename Tr=std::char_traits<Ch> >
> class std::basic_ostream : virtual public std::basic_ios<Ch,Tr>
> {
>  basic_ostream &write_binary(const void *buf, size_t n);
>  // write n bytes (not characters) from buf.

When Ch == char, how does this differ from the

  basic_ostream& basic_ostream::write( char_type const* str,
                                       streamsize count );

function?  After all, a char *is* a byte.  And when Ch != char, what,
if anything, defines how the bytes are mapped into multibyte
characters? (This requires knowledge of endianess, and hence either
invokes implementation defined behaviour or requires the Standard to
mandate a particular endianess.)

I really don't see what this would gain that isn't already there.

--
Richard Smith

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: richard@ex-parrot.com (Richard Smith)
Date: Sun, 23 May 2004 00:13:38 +0000 (UTC)
Raw View
kprateek88@yahoo.com (Prateek R Karandikar) wrote in message news:<607f883e.0405201123.718c18a6@posting.google.com>...
> Why aren't there functions for binary I/O? The possible functions
> could be:
>
> template<typename Ch, typename Tr=std::char_traits<Ch> >
> class std::basic_ostream : virtual public std::basic_ios<Ch,Tr>
> {
>  basic_ostream &write_binary(const void *buf, size_t n);
>  // write n bytes (not characters) from buf.

When Ch == char, how does this differ from the

  basic_ostream& basic_ostream::write( char_type const* str,
                                       streamsize count );

function?  After all, a char *is* a byte.  And when Ch != char, what,
if anything, defines how the bytes are mapped into multibyte
characters? (This requires knowledge of endianess, and hence either
invokes implementation defined behaviour or requires the Standard to
mandate a particular endianess.)

I really don't see what this would gain that isn't already there.

--
Richard Smith

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: tom_usenet@hotmail.com (tom_usenet)
Date: Tue, 25 May 2004 16:02:10 +0000 (UTC)
Raw View
On Fri, 21 May 2004 23:24:03 +0000 (UTC), sebormartin@netscape.net
(Martin Sebor) wrote:
 They may
>also need to do a character conversion between the internal encoding
>(e.g., wchar_t) and the external representation (invariably char).

Not if you write a streambuf to output to a unicode GUI component...

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]