Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem has nothing to do with <code>bash</code>, or with the C++ code. To verify this, this trivial program has the same problem:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; int main(int, char**) { std::string s; std::getline(std::cin, s); } </code></pre> <p>And so does this Python program:</p> <pre><code>#!/usr/bin/python raw_input() </code></pre> <p>And you can try running under different shells and see that it makes no difference.</p> <p>The problem is that a TTY in line mode always has a max line length, and this one happens to have a max line length of 1024.</p> <p>So, this isn't even really a programming question; it's a TTY question. Which is why you can find a dup over at <a href="https://superuser.com/questions/264407/macosx-10-6-7-cuts-off-stdin-at-1024-chars">superuser.com</a> instead of here. See the discussions there on the various ways to deal with this.</p> <p>However, if you want to deal with it from within your program, rather than globally, you can check whether <code>stdin</code> is a <code>tty</code>, and if so use <code>termios</code> functions to temporarily turn off line mode. For example, something like this:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;termios.h&gt; #include &lt;unistd.h&gt; int main(int, char**) { struct termios told; if (isatty(STDIN_FILENO)) { tcgetattr(STDIN_FILENO, &amp;told); struct termios tnew = told; tnew.c_lflag &amp;= ~ICANON; tcsetattr(STDIN_FILENO, TCSAFLUSH, &amp;tnew); } std::string s; std::getline(std::cin, s); if (isatty(STDIN_FILENO)) { tcsetattr(STDIN_FILENO, TCSAFLUSH, &amp;told); } } </code></pre> <p>However, you'll obviously want real error handling, and you'll probably want to use RIAA to stash and restore the original termios settings, and you should probably read <code>man termios</code> to understand what you're doing and what other effects it will have (and whether you want to do anything else).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload