Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string gets truncated after 4095 characters froms std::cin

My programs needs to read up to 50k character long strings from stdin. The code is as follows:

#include <iostream>
#include <string>

std::string A;

int main(){
    std::cin >> A;
    std::cout << "String max: " << A.max_size() << std::endl;
    std::cout << "Size: " << A.size();

When I try to enter a 10k character long string I get the following output:

String max: 4611686018427387903
Size: 4095

According to Google both std::cin and std::string should have no problem handling 10k characters, but for some reason A gets truncated after 4095 characters. I entered the string by pasting it into the default Ubuntu terminal. Pasting it into Python3 in the same terminal works fine, which leads me to believe that it's not the terminal that's truncating it, but C++. I compiled with g++ program.cxx and I have 16 GB of RAM.

How can I enter large strings from stdin? Any help is appreciated.

P.S.: If you need a large string just paste this into Python: print("123"*5000)

like image 542
Jan Avatar asked Oct 17 '25 17:10

Jan


1 Answers

You're probably running into the 4096 line limit input of the Linux terminal when in canonical mode. If you try to enter a very long line (>4095 chars) into a Linux terminal, the excess will be discarded until you enter a newline or eof character to flush the terminal buffer.

There are a couple of ways you can work around this:

  • insert an eof/flush char (usually ctrl-D) every so often in the input -- each such character will flush the terminal buffer, resetting that 4K limit, and allowing a longer line to be entered. Be careful not to insert one immediately after a newline or another eof, as that will cause an EOF on the input
  • put the terminal into non-canonical mode. This will cause the buffer to be flushed whenever a process reads it; most processes will generally read much faster than characters can be typed or pasted into the terminal, so you'll never come close to the 4K limit.
like image 77
Chris Dodd Avatar answered Oct 19 '25 10:10

Chris Dodd