Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Opening an I/O stream from a string.

55 views
Skip to first unread message

Doug Mika

unread,
Mar 23, 2015, 3:24:21 PM3/23/15
to
Hi to all

I'm reviewing my C++ notes, and I noticed that I can't open a file for I/O from a string variable. For example

string fname;
ofstream out;

cout<<"Enter the output file name:";
cin>>fname;
out.open(fname);

Why not, how do I get around it?

Thanks

Doug Mika

unread,
Mar 23, 2015, 3:31:05 PM3/23/15
to
I found it, you have to use fname.c_str();

Melzzzzz

unread,
Mar 23, 2015, 3:38:52 PM3/23/15
to
String is not included to reduce dependencies , you call fname.c_str().

>
> Thanks


Ian Collins

unread,
Mar 24, 2015, 12:29:16 AM3/24/15
to
Use a newer compiler. The missing open member and constructor have been
added in C++11.

--
Ian Collins

Vir Campestris

unread,
Mar 24, 2015, 5:39:41 PM3/24/15
to
On 24/03/2015 04:29, Ian Collins wrote:
> Use a newer compiler. The missing open member and constructor have been
> added in C++11.

Ooh. Does this mean you can now open Unicode filenames?

Andy

Bo Persson

unread,
Mar 25, 2015, 2:20:56 PM3/25/15
to
Maybe.

The file names depend much more on the operating system than on the
programming language.


Bo Persson


Vir Campestris

unread,
Mar 25, 2015, 5:52:55 PM3/25/15
to
I still haven't got around to checking - but STL used to require narrow
char strings for the filenames. This was OK for Unix-like systems that
use UTF-8 - but not for Windows where native filenames are 16-bit char
Unicode, and the glue layer for 8-16 bit defaults to your national
character set.

I suspect that it working OK on Unix/Linux may be why it got past the
committee.

Andy

0 new messages