Topic: ofstream and write() function
Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/07/04 Raw View
Valentin Bonnard <bonnardv@pratique.fr> writes:
|> James Kanze <james-albert.kanze@vx.cit.alcatel.fr> writes:
|>
|> > mhc@slip.net (Michael H. Chen) writes:
|> >
|> > |> Hi, I would like to know why the ofstream function write() has
|> > |> to take a char* as its first argument.
|> >
|> > It seems funny to me, as well. I would expect either void const* or
|> > unsigned char const*.
|>
|> A would prefer void const*, because the reinterpret_cast doesn't
|> tell you anything interresting (you know that you are doing
|> unformated output anyway).
|>
|> At least unsigned char const* makes some sens, as I don't
|> think that accessing an object as [no explicit signess] char
|> is portable.
It's one of the awkward situations in C (and thus in C++). What you are
passing is a pointer to raw memory. Logically, a pointer to raw memory
should be void*, and that is really what I would expect at the
interface.
On the other hand, you cannot access the raw memory through a void*; you
must cast it. According to the C standard (with the TA's, etc.), the
ONLY type which is guaranteed to work correctly on raw memory is
unsigned char. (Plain char can be signed, and e.g. on a 1's complement
machine, accessing a signed value might "normalize" the 0's, converting
a 0xff to 0x00. Unused bits are also permitted.)
What we obviously need is a specific type for raw memory. (20-20
hindsight, although Modula-2 had one.)
--
James Kanze home: kanze@gabi-soft.fr +33 (0)1 39 55 85 62
office: kanze@vx.cit.alcatel.fr +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
-- Conseils en informatique industrielle --
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Richard See <Richard.See@vega.co.uk>
Date: 1997/07/04 Raw View
Valentin Bonnard wrote:
>
> A would prefer void const*, because the reinterpret_cast doesn't
> tell you anything interresting (you know that you are doing
> unformated output anyway).
>
> At least unsigned char const* makes some sens, as I don't
> think that accessing an object as [no explicit signess] char
> is portable.
>
...
>
> What about ?
>
> void write (const charT*, size_t);
> void write (const void*, size_t);
>
I can see two interesting issues, sizeof(charT) and how easy it is to
convert to const charT*. One advantage of using an ((un)signed) char
is that sizeof(charT) == 1, so the meaning of the sizeof argument is
clear, and pointer arithmetic can easily be performed on the pointer
that is being supplied (easy to implement write hardly matters, but
there's a fair chance that the user will be wanting to do pointer
arithmetic too.)
Using const void* gains neater casting of pointers, but then loses the
ability to do pointer arithmetic, since sizeof(void) is an illegal
operation.
Using (for example) const wchar_t* may cause confusion as to the meaning
of the size_t argument: is it size in bytes, or the number of wchars?
The function becomes slightly harder to use than the const char*
equivalent, as conversions by a factor of sizeof(wchar) may need to be
performed.
I find having to cast to const char* slightly awkward, but at least it
reminds me that I ought to be thinking in bytes if I'm writing raw data.
Richard.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/07/03 Raw View
James Kanze <james-albert.kanze@vx.cit.alcatel.fr> writes:
> mhc@slip.net (Michael H. Chen) writes:
>
> |> Hi, I would like to know why the ofstream function write() has
> |> to take a char* as its first argument.
>
> It seems funny to me, as well. I would expect either void const* or
> unsigned char const*.
A would prefer void const*, because the reinterpret_cast doesn't
tell you anything interresting (you know that you are doing
unformated output anyway).
At least unsigned char const* makes some sens, as I don't
think that accessing an object as [no explicit signess] char
is portable.
> |> From what I understand, the write() takes two arguments,
> |> the first being the starting point from which to write
> |> and the second being the number of bytes to write.
> |>
> |> Assuming I have a struct that I wish to be written, why can't
> |> I just send it by address?
>
> Because the function would have no way of knowing the length.
What about ?
void write (const charT*, size_t);
void write (const void*, size_t);
template <class T>
void write (const T*);
--
Valentin Bonnard
mailto:bonnardv@pratique.fr
http://www.pratique.fr/~bonnardv (Informations sur le C++ en Francais)
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: mhc@slip.net (Michael H. Chen)
Date: 1997/06/26 Raw View
Hi, I would like to know why the ofstream function write() has
to take a char* as its first argument.
Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/06/28 Raw View
mhc@slip.net (Michael H. Chen) writes:
|> Hi, I would like to know why the ofstream function write() has
|> to take a char* as its first argument.
It seems funny to me, as well. I would expect either void const* or
unsigned char const*.
|> From what I understand, the write() takes two arguments,
|> the first being the starting point from which to write
|> and the second being the number of bytes to write.
|>
|> Assuming I have a struct that I wish to be written, why can't
|> I just send it by address?
Because the function would have no way of knowing the length.
|> From what I understand, I have to explicitly cast the struct
|> to char* in the write(). That being the case, I'm trying to
|> make sense out of how it would access it as a char*
The function write just dumps the bytes, without any concern as to what
they mean. In practice, this is almost never what you want, unless you
have previously streamed the structures into an array of unsigned char.
--
James Kanze home: kanze@gabi-soft.fr +33 (0)1 39 55 85 62
office: kanze@vx.cit.alcatel.fr +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
-- Conseils en informatique industrielle --
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]