2

I want to let the users input strings with more than 4,095 characters, but std::cin/std::istream seems to have some sort of undocumented limit and keeps cutting off the string to only allow 4,095 characters.

Is there some way to get past this "limit" using one of the many input functions, preferably allowing any length? There might not be a limit in the cpp standard, but the operating system I am currently using may have an input buffer limit - Arch Linux.

The strings may reach lengths of 1 ≤ L ≤ 10e6 where L is the length of the string.

int main() {
    /* The user inputs a string with more than
       4095 characters. 

       Let's say that every character in the string
       is 'a' except for the 4095th character in the 
       string, which is 'B', and the end of the string 
       looks like "...aaBaaaaaaaaaaaaa"
       
       The string will get cut off to "...aaB" (4095 
       characters) meaning that there seems to be some 
       sort of limit. */

    std::string str;

    /* Manually paste a string that has 10,000 
       characters into a terminal emulator (Alacritty)
       with CTRL+C → SHIFT+INSERT */

    std::cin >> str;

    /* This next line is supposed to output 10,000,
       but instead, it outputs 4,095. */

    std::cout << str.length() << std::endl;
}

The provided example should be simple enough to understand without needing a better reproducible example. Providing a better example would only clutter the post with a lot of unneccessary information since it should be simple enough as it is.

The input data I give it looks like this aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa but imagine 10,000 a's and it does not have any whitespaces.

Note: This program is compiled with this command:

g++ -g -O2 -std=gnu++17 -static light.cpp && ./a.out
ilittlebig
  • 31
  • 4
  • 2
    This is likely a limit imposed on your operating system. `std::istream` has no limitations in of itself. It can't. All it is, is a source of bytes. It doesn't know anything about lines. The file it reads is text or binary. It doesn't care. – Sam Varshavchik Oct 30 '21 at 19:46
  • 3
    Edit the question to provide a [mre]. The sample code shown does not output anything, so there is no way to tell how many characters it read, unless you are observing something in the debugger. State the output you observe (from the program or the debugger) that makes you think only 4095 characters are being read. State which operating system you are using, and which compiler, and so on. State whether the input comes from a file (e.g., redirected by the shell to the program’s standard input) or elsewhere. – Eric Postpischil Oct 30 '21 at 20:33
  • 3
    I am on Arch Linux, and your program gives `10000` for me. `for ((i=0;i<10000;++i)); do echo -n 'a'; done | ( printf "%s\n" "#include " "int main() { std::string str; std::cin >> str; std::cout << str.length() << std::endl; }" | g++ -xc++ - && ./a.out ) ` outputs 10000. Are you _sure_ there are no whitespaces in your input? Or maybe you set `std::cin << setw` before call? What compiler, it's options and version, standard library veresion are you using? `into a terminal emulator` What terminal emulator are you using? How are you pasting the input "manually"? – KamilCuk Oct 30 '21 at 21:09
  • 2
    So what does `for ((i=0;i<12345;++i)); do echo -n 'a'; done | ./a.out` print? If you type in your terminal emulator `echo '` and then "manually paste a string" and then type `' | wc` - what would `wc` output? – KamilCuk Oct 30 '21 at 21:16
  • The first part gave me the output `12345` and the second part gave me the output `1 1 10001` and that was with a string with the length `10000`. – ilittlebig Oct 30 '21 at 21:23
  • 1
    https://unix.stackexchange.com/questions/131105/how-to-read-over-4k-input-without-new-lines-on-a-terminal – Jeff Garrett Oct 30 '21 at 22:01
  • 1
    Does this answer your question? [std::string gets truncated after 4095 characters froms std::cin](https://stackoverflow.com/questions/68765094/stdstring-gets-truncated-after-4095-characters-froms-stdcin) – JaMiT Oct 31 '21 at 01:23
  • You still have not provided a [mre], and so your question has been closed. In particular, you have not answered questions about how you are supplying input to the program. If you are attempting to paste text, as by pressing control-V, then the problem is explained by limitations of the terminal software, which is separate from C++ and its I/O buffers. If you are supplying input from a file, as by redirection, the cause is something else. So answering questions is needed. Do not withhold information. – Eric Postpischil Oct 31 '21 at 09:05

0 Answers0