View Full Version: Limit characters with std::getline?

C++ Learning Community > C++ Help > Limit characters with std::getline?


Title: Limit characters with std::getline?
Description: Am I missing something?


Ravotus - January 11, 2007 05:10 PM (GMT)
I'm trying to use std::getline to read a line from an input stream into an std::string. I want to be able to limit the number of characters read, though. It appears that the std::getline function cannot do this. However, std::istream::getline accepts a char* and buffer size as parameters, meaning I could limit it that way.

Is there a way I could get the functionality of reading directly into an std::string and still be able to limit the total length? Or would it be better for me just to allocate some memory and pass that to std::istream::getline?

Thanks for the help. ^_^

C-Man - January 11, 2007 07:03 PM (GMT)
it depends on how you want to treat what is left of the line when it doesn't fit
i suggest using std::string::substr , but there is no known way (to me) to limit how much the user can input (using only standard c++) since the input buffer is independent of the functions you use to read it

myork - January 11, 2007 07:09 PM (GMT)


Do you want to read a fixed 'size' object.
Or do you want to read a line upto a 'size' then stop.
Or do you want to read a line and discard everything from 'size' to the end of line.

Ravotus - January 11, 2007 09:23 PM (GMT)
Number two. :P

If I'm not mistaken, streams are read character by character, correct? As long as the entire line isn't read in one go, it should be possible.

Shonoby - January 11, 2007 10:22 PM (GMT)
well iam not sure what u mean but there is a way to limit what the user can input by using:
CODE

char word[ x ];
x = 10
cin >> std::setw( x ) >> word

this limits the size of input to what ever 'x' is, but it cuts if off so u could make a
if stru. that deciphers whether it is over the limit or not.
which is found in header file '#include <iomanip>'

But if u want to read a certain amount of characters just use something like this:
CODE

for( int x; word[] != '/0'; x++ )   // '/0' is the same as NULL
       cout << word[ x ];

C-Man - January 11, 2007 10:24 PM (GMT)
Not exactly possible

[User]->[Input buffer]->[Streams]

[Input buffer] has no line checking or anything so if it's empty and input is needed you just type in until you hit enter you can't regulate that
all you can regulate is how stream reads from the input buffer


Shonoby - January 12, 2007 12:31 AM (GMT)
Ohh, i see i see but, i don't know about that..........

TheHawgMaster - January 12, 2007 04:26 AM (GMT)
Why not just get the input one character at a time and stop when foo is pressed? Then you can monitor the input yourself and stop after a certain number of characters.

istream::get + string::append should do the trick. Might be a bad solution though, I'm a bit rusty :P

Ravotus - January 12, 2007 05:42 AM (GMT)
Well, I actually decided to go a different route, so I guess that I don't need an answer anymore. Thanks though.

BTW, why was everyone thinking I'm talking about a keyboard input stream or something? I'm reading from a streambuffer from a socket. :lol:

C-Man - January 12, 2007 06:18 AM (GMT)
oh i see O.o




Hosted for free by InvisionFree