OSDN Git Service

901701ff4805651e2c5231102f38e489f0c22c29
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / docs / html / 27_io / binary_iostreams_kuehl.txt
1
2 From: kuehl@ramsen.informatik.uni-konstanz.de (Dietmar Kuehl)
3 Newsgroups: comp.std.c++
4 Subject: Re: binary iostreams ?
5 Date: Sat,  3 Feb 2001 17:17:49 GMT
6 Message-ID: <95hctq$suu$2@news.BelWue.DE>
7
8 Hi,
9 Plinio Conti (plinio.contiNO@SPAMMINGmclink.it) wrote:
10 : Why std c++ library stream classes are only text-oriented?
11
12 There is only a text oriented front end to stream buffers because text
13 input and output does not vary between platforms. This is very
14 different for binary output. For example, binary output has to consider
15
16 - word sizes: Is an 'int' two, four, or eight bytes long? The same
17   questions arise for all other built-in types.
18
19 - what is the bit pattern of a value? I think that at least implicitly
20   in the standard a binary representation for integer types is required.
21   I don't think that it is required to use two's complement. In any
22   case, the floating point representations do differ, eg. in their
23   number of bytes used.
24
25 - what "endianess" is to be used?
26
27 Basically it is possible to decide a format for each of those. This,
28 however, implies inefficient implementations on platforms where the
29 format does not match the internal representation.
30
31 What many people asking for binary I/O forget is that binary I/O also
32 requires some form of formatting! Assuming that just writing data and
33 then reading it in will work is asking for problems, eg. when the
34 compiler version changes and they decided to use a 32 bit integer
35 rather than a 16 bit integer: It is not even necessary to switch
36 platforms to run into problems!
37
38 : I mean, if I want to write an int, a float, etc. AS IT IS I can't use
39 : streams, because they write and read a human readable text format of
40 : numbers.
41
42 Which is for most I/O a reasonable approach. If it is not for you, you
43 might want to consider a data base: File I/O is not really useful as a
44 persistance mechanism. It is fine eg. for user interaction (text I/O),
45 logging (text I/O), cross platfrom program interaction (formatted I/O),
46 and data exchange (formatted I/O). In all these cases, the I/O is
47 formatted, although possible using a binary format. For persistance,
48 data bases are used. Depending on your needs, a relational or an object
49 oriented one may be better suited.
50
51 That said, it is worth to mention that it is easy to create a hierarchy
52 similar to IOStreams built on top of stream buffers but doing binary
53 formatting. A somewhat aged example is found at
54 <ftp://ftp.fmi.uni-konstanz.de/pub/algo/personal/kuehl/binio.tar.gz>.
55 This uses XDR formatting of the binary data (well, if I remmeber 
56 correctly, it is easy to plug in a different binary formatting).
57
58 : Does anyone know how to solve the problem?
59
60 Use a data base, text formatting, or binary formatting. With the
61 details you have given it is impossible to tell which of those is the
62 right approach because you haven't told *why* you want a binary format
63 and *what* you want to do. That basically means that you came up with
64 solution and you want us to confirm that it is the right one without
65 telling us what problem is solved! Until I have seen the problem I
66 doubt that binary I/O is the right approach...
67
68 ... and, BTW, using 'std::istream::read()' and 'std::ostream::write()'
69 is almost certainly the *wrong* approach! These functions are an
70 historical mistake which should have been corrected in the standard:
71 It is my understanding that these methods were present in the IOStream
72 version predating the rework from Jerry Schwartz and were left in to
73 be compatible with the earlier stuff although they were not necessary:
74 You could get binary I/O from the stream buffer level. The original
75 IOStream library (maybe you remember using <stream.h>) did not have
76 stream buffers and thus basic support for binary I/O was also present
77 on the streams level.
78
79 : What do you think about this choice?
80
81 When I wrote the above paragraph about confirming your choice, I haven't
82 read this question! As I said above: You told us what solution you have
83 choosen without stating what problem is solved. We cannot determine
84 whether your choice is the right one. Actually, I'm pretty sure it is
85 the wrong one but without seen the details I can't be certain.
86 --
87 <mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
88 Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
89