🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3845

fstream constructors and strings

#3845
From: Kresimir Fresl
Date: Thu, 28 Mar 1996 13:22
44 lines
2638 bytes
Why are there no ifstream and ofstream constructors that take a
std::string rather than plain char* for the ``file name''?

The following `idiom' was, I think, often used (I use `S', and not `s', 
because this is not a proposed standard string class):

     cout >> "Enter file name -> ";
     String file_name;
     ifstream (file_name); // or: ofstream (file_name);

Declaring file_name as String, instead of char[file_name_length], 
one does not have to worry about reserving space for file name
(file_name_lengths are very different on different operating systems). 

The call to istream/ostream constructor is possible because String 
classes use to have:
    String::operator const char*() const
    
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 have constructors that take strings.

In many C++ books we can read that plain C arrays (including char[]s 
and char*s) are to low level, and that we should use vector and string 
classes. Also, it is supposed that iostream classes and operators are 
`on the higher level of abstraction' than cstdio types and functions.
So, why do we have to manualy convert `high level constructs' to `low 
level constructs'?


Kresimir Fresl
Faculty of Civil Engineering, Zagreb, Croatia
fresl@master.grad.hr
---
[ 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                             ]
Technical Details
Message-ID: <315A8E72.1D3C@master.grad.hr>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back

Show Full Headers
Complete Article Headers
From: Kresimir Fresl <fresl@master.grad.hr>
Message-ID: <315A8E72.1D3C@master.grad.hr>
X-Original-Date: Thu, 28 Mar 1996 14:04:50 +0100
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 28 Mar 96 13:22:49 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Organization: Faculty of Civil Engineering, Zagreb, Croatia
X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.01 9000/715)
Cc: Kresimir.Fresl@public.srce.hr
Subject: fstream constructors and strings
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMVqS1eEDnX0m9pzZAQFW5wF/d6F1QtZ+J5PbKO0Y34o46D8jySacR07M
 i1eblh5iWLPvMByaeiXbiJk5DnmP43y3
 =AW+2