Click to See Complete Forum and Search --> : Can someone who knows C++....
TheLinuxDuck
04-24-2001, 05:03 PM
Help me with a dilemma??
I'm looking for info on using the string class. I was writing a c++ file copy utility (to learn) and was forced to use a char*.
I'd rather not, since that type of variable kinda defeats the purpose of OOD.
Can someone show me how to read a line of text or data from a file, and store it into a string variable?
I would greatly appreciate! (and I'll prolly put the example on the CCAE site, too)
Bradmont
04-24-2001, 05:07 PM
The functions are:
getline(istream & in, string & foo)
getline(istream& in, string & foo, char terminatingcharacter)
terminatingcharacter defaults to a newline, but u can use anything else.
Use them like this:
ifstream in; //open it and stuff, can use any istream
string foo;
getline(in, foo); // gets a whole line up to a '\n'
getline(in, foo, '_'); //gets everything up to the next "_"
<edit> I guess it would make sense to use code tags her... :rolleyes:</edit>
<edit> Bah, I suck. This is the last time I'll edit it... no, really... </edit>
[ 24 April 2001: Message edited by: Blargmont ]
TheLinuxDuck
04-24-2001, 05:16 PM
Blargmont:
Does that version of getline leave the terminating character on the read line, or does it remove it, like the ifstream getline does?
Does that getline have a max length of how many chars it will read at a time? Will string will itself up no matter (memory willing)?
Thanks for the info though, it looks like that's going to be the thing I need.. (^:
Bradmont
04-24-2001, 05:18 PM
It does strip the terminating character. I don't know if it has any size limit. I don't think it does (but it'd be pretty easy to test ;))
[ 24 April 2001: Message edited by: Blargmont ]
Stuka
04-24-2001, 05:42 PM
Interesting question here Bradmont - is that getline a nonstandard extension? I can't find any reference to it in M$VC++ 6.0 (I know, I know :)). However, there are some interesting classes (istrstream, istringstream) that I'm trying to figure out how to use them now...
TheLinuxDuck
04-24-2001, 05:42 PM
Rock on! I like things that I like, like that.
Btw, do they update gcount, as well?
TheLinuxDuck
04-24-2001, 06:06 PM
For information's sake, I found out something poking around in the bastring.h file..
If you have a string class, and need to access it as a char *, you can simply say (example):
ifstream inFileStream(filename.data());
The data() function member of the string class returns a pointer to the actual data of the string class.. as it implies.. (^: very qool.. I was needing a way to do that, and voila, there it is!!
Thanks for the help, blarg!! (^: C++ isn't my greatest programming skill.. but I think I'd like to get into it some more...
Peace!
lazy_cod3R
04-24-2001, 06:58 PM
i also think you can do
string theString="a_string";
then do
theString.c_str() to return a pointer to the actual char* its self.
Bradmont2
04-24-2001, 09:45 PM
I'm pretty sure .data() and .c_str() do exactly the same thing.
And I'm pretty sure getline is standard, I know it works in VC++ 6 and GCC... its either in string or iostream, not sure which...
Strike
04-24-2001, 10:32 PM
It's in iostream because it's a member of the cin iostream. cin.getline does the same thing, just assuring that it's from STDIN (unless cin can be redefined, and I forget if it can ... other than through redirection of course)
Stuka
04-25-2001, 10:45 AM
Strike, Blarg(Brad, Brad2? :)),
I know getline is in there, but when I tried to use it (like your example there), I get invalid parameter errors for the string arguments...any hints?