Topic: iostreams and strings (Was: string -> const char* conversion operator)
Author: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1996/11/14 Raw View
Hi,
Kresimir Fresl (fresl@grad.hr) wrote:
: Why are there no ifstream and ofstream constructors with
: string parameters rather than plain char*s for the ``file name''?
I think this is an error which has to be fixed. It can be made an issue
in the three month following the public release of the next CD. Maybe
someone knowing the exact process of this can post the rules or a
pointer to the rules for raising an issue in advance...? I guess, I
have to find out the rules for the national body where I have to raise
issues, i.e. DIN, myself but knowing the ANSI rules would still be
useful for a lot of people. OTOH, if everybody knows how to raise an
issue, the committee might get that much issues raised that the
standard is delayed...
: Therefore, I think that fstream classes, beside (or even instead
: of) constructors with const char* arguments, should have constructors
: with string arguments.
I think, the 'char const *' constructors and open functions should be
replaced by functions using 'string', maybe augmented by versions
taking a 'wstring'. I guess, to effectively raise an issue, it would be
necessary to think of the exact modification to be done and not to
leave all the work to the committee. The questions arising here are,
whether the open function should continue to accept 'char const *' or
accept only objects of type 'string' (I guess for backward
compatibility it would be necessary to support 'char const *' function,
maybe as being deprecated), and whether there should also be functions
accepting arguments of type 'wstring' or even member templates
accepting 'basic_string<T, Traits>'s. I guess, it would be reasonable
to restrict the modification to accept a 'string' but then I don't know
how those feel who use 'wstring's for their normal strings due to the
encoding they use. While it is obvious that the open functions using a
'string' can be implemented everywhere where they can be implemented
using a 'char const *', I don't know whether this also holds for
'wstring'.
--
<mailto:dietmar.kuehl@uni-konstanz.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ 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: Kresimir Fresl <fresl@grad.hr>
Date: 1996/11/12 Raw View
Why are there no ifstream and ofstream constructors with
string parameters rather than plain char*s for the ``file name''?
With some non-standard String class, which has String to
const char* conversion operator, one can write:
cout << "Enter file name -> ";
String file_name;
cin >> file_name;
ifstream in_file (file_name);
// or: ofstream out_file (file_name);
That was, I believe, often-used `idiom'.
Because there's no implicit conversion from standard string to
char*, one must write:
ifstream in_file (file_name.c_str());
I found this annoying.
Authors of some articles in `string -> const char* conversion
operator' thread gave convincing arguments against that operator,
and I do not propose to add it.
But, in many articles in this newsgroup (and in c.l.c++.mod.)
there were advices and recommendations to use vectors instead of
C arrays, because C arrays are very low level, error prone and
inflexible; IMHO the same holds for strings vs char*s.
So, why do we have to manualy convert high level to low level
language constructs?
Therefore, I think that fstream classes, beside (or even instead
of) constructors with const char* arguments, should have constructors
with string arguments.
Kresimir Fresl
fresl@master.grad.hr
PS. This is the third time that I ask similar/same question
(first in this newsgroup, and then in c.l.c++.mod.). I
received only one reply, by Sean L. Palmer. Is this really
such a minor issue?
I received Mr. Palmer's reply by e-mail; AFAIK it never
appeared in c.l.c++.mod. I think that part of his reply
should be quoted here:
``I agree that the streams classes should take string
parameters as well as const char*'s for filenames. string
ought to be a useful enough class that user code can use it
exclusively, without the need for any use of const char* or
conversion functions except in interfacing to other older code
(such as the Win32 API.) In essence, anywhere in the standard
library that a const char* is valid, string ought to be valid
as well. They can provide string-ized overloads of all C standard
library routines and STL routines.''
---
[ 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 ]