I have code that looks like this
string inputString;
std::getline (std::cin, inputString)
cout << inputString;
when I cin a long string ~30k characters then inputString only has part of that string. Any ideas how to fix this?
From the std::getline()
documentation on cppreference.com :
getline
reads characters from an input stream and places them into a string:1. Behaves as
UnformattedInputFunction
, except thatinput.gcount()
is not affected. After constructing and checking the sentry object, performs the following:
Calls
str.erase()
Extracts characters from
input
and appends them tostr
until one of the following occurs (checked in the order listed)a. end-of-file condition on
input
, in which case,getline
setseofbit
.b. the next available input character is
delim
, as tested byTraits::eq(c, delim)
, in which case the delimiter character is extracted frominput
, but is not appended tostr
.c.
str.max_size()
characters have been stored, in which casegetline
setsfailbit
and returns .If no characters were extracted for whatever reason (not even the discarded delimiter),
getline
setsfailbit
and returns.2. Same as
getline(input, str, input.widen('\\n'))
, that is, the default delimiter is the endline character.
So check to see if your input is being limited by the value of inputString.max_size()
. It might be near 32k on your system.
By default, the terminal works in canonical mode, and it has a buffer of the length 4096 Bytes. So that's why the maximum input that the string can take is 4095. Now the solution is to change to the noncanonical mode in Linux. You can do so by
$ stty -icanon # for switching to the noncanonical mode.
$ stty icanon # reverting back to the canonical mode when you are done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.