Topic: getline into string?
Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: 1995/04/02 Raw View
In article <D64DDy.MHx@hplabsz.hpl.hp.com>,
Bob Fraley <fraley@hpl.hp.com> wrote:
>A couple of days ago I was having a discussion about C++ strings vs
>char arrays, and decided that it was time to mend my ways, and abandon
>char arrays for the promises of strings. My goal was to write
>programs with no visible use of char* whatsoever, if possible. The
>exception, of course, is char array constants, but the program reader
>should be unaware that they aren't string constants.
>
>Things went pretty well until I tried to read a line using
>cin.getline. I couldn't find a way using Borland of doing this
>without having a separate char array, or possibly exposing the
>internals of a string using c_str. I was also unsuccessful finding
>anyway to do it in the current C++ draft.
>
>Does anyone know of a way to do this?
Of course. You use an array so short it isn't an array.
One character long.
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd,
81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
NSW 2037, AUSTRALIA Phone: 61-2-566-2189
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 28 Mar 1995 17:09:58 GMT Raw View
In article MHx@hplabsz.hpl.hp.com, fraley@hpl.hp.com (Bob Fraley) writes:
>
>... Things went pretty well until I tried to read a line using
>cin.getline. I couldn't find a way using Borland of doing this
>without having a separate char array, or possibly exposing the
>internals of a string using c_str. I was also unsuccessful finding
>anyway to do it in the current C++ draft.
At the last C++ Committee meeting we approved a getline function
for strings. Coming soon(er or later) to a compiler near you.
---
Steve Clamage, stephen.clamage@eng.sun.com
Author: pstemari@erinet.com (Paul J. Ste. Marie)
Date: Wed, 29 Mar 95 03:31:46 GMT Raw View
In article <D64DDy.MHx@hplabsz.hpl.hp.com>,
fraley@hpl.hp.com (Bob Fraley) wrote:
: [...]
:Things went pretty well until I tried to read a line using
:cin.getline. I couldn't find a way using Borland of doing this
:without having a separate char array, or possibly exposing the
:internals of a string using c_str. I was also unsuccessful
:finding anyway to do it in the current C++ draft.
:
:Does anyone know of a way to do this?
Not having a draft to look at the standard string definition, I can
only guess what they support, but the RogueWave string class has a
member function that accepts an istream& and reads in a line, or
you can write an istream& operator>>(istream&, string&) that has
whatever behavior you desire.
No matter what, unless you assume the input is seekable, you're
stuck with allocating and possibly reallocating memory until you've
got enough space to hold the input. Whether that's done internally
to the string class or externally doesn't make a big difference,
although doing it inside the string class avoid a second allocation
and copy.
I've wanted a string class with a donate_buffer(char *) member
function for some time, just to avoid the second reallocation when
doing stuff like this.
--Paul J. Ste. Marie, pstemari@well.sf.ca.us, pstemari@erinet.com
The Financial Crimes Enforcement Network claims that they capture every
public posting that has their name ("FinCEN") in it. I wish them good hunting.
Author: fraley@hpl.hp.com (Bob Fraley)
Date: Mon, 27 Mar 1995 22:05:58 GMT Raw View
A couple of days ago I was having a discussion about C++ strings vs
char arrays, and decided that it was time to mend my ways, and abandon
char arrays for the promises of strings. My goal was to write
programs with no visible use of char* whatsoever, if possible. The
exception, of course, is char array constants, but the program reader
should be unaware that they aren't string constants.
Things went pretty well until I tried to read a line using
cin.getline. I couldn't find a way using Borland of doing this
without having a separate char array, or possibly exposing the
internals of a string using c_str. I was also unsuccessful finding
anyway to do it in the current C++ draft.
Does anyone know of a way to do this?
Thanks.
Bob Fraley
Hewlett-Packard