Article View: comp.std.c++
Article #3780fstream constructor and strings
From: Kresimir Fresl
Date: Fri, 22 Mar 1996 18:17
Date: Fri, 22 Mar 1996 18:17
44 lines
2353 bytes
2353 bytes
I think that the following `idiom' was often used:
// necessary includes
cout >> "Enter file name -> ";
String file_name;
ifstream (file_name); // or: ofstream (file_name);
(I used `S' (not `s') because this is not a proposed standard string
class.)
ifstream constructor expects const char*, but above call was possible
because String classes (eg. in libg++) used to have:
String::operator const char*() const
(or something similar).
According to proposed standard, one should write:
ifstream (file_name.c_str());
because implicit conversion of string to const char* is not defined.
I agree that implicit conversion is (almost always) dangerous, and I
_do not_ propose to add conversion operator to string class. Instead, I
think that fstream classes (beside, or even instead of, constructors that
take const char*s) should also have constructors that take strings.
In many C++ books one can read that plain C arrays (including char[]s and
char*s) are to low level, and that with classes (eg. vectors and strings)
one can work on higher levels of abstraction. fstreams are `better' than
cstdio functions, but, because of const char* parameters, they are still
to low level.
fresl
-----------
Kresimir Fresl
Faculty of Civil Engineering, Zagreb, Croatia
email: fresl@master.grad.hr
---
[ 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
]
Technical Details
Message-ID:
<9603221244.AA07167@master.grad.hr>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: fresl@master.grad.hr (Kresimir Fresl) Newsgroups: comp.std.c++ Subject: fstream constructor and strings Date: 22 Mar 1996 10:17:28 PST Organization: ? Approved: austern@isolde.mti.sgi.com Message-ID: <9603221244.AA07167@master.grad.hr> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: Fri, 22 Mar 96 13:44:10 +0100 Content-Length: 1338 X-Lines: 41 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMVLvI0y4NqrwXLNJAQGkBAH/f0rM1U+gWdKJXy4486RmUOgbGyQ5CAOb DyZ/3/2L86MLLcrbDfqaPOXTYpFc/V7mp1KVYbb5CfgZ0TPx6CteOA== =oZXn Originator: austern@isolde.mti.sgi.com