On os X 10.8, I tried to redirect std input to c++ string via bash.
After I printed 1024th character, I even cannot press enter.
Could you please explain where the problem occurs and how to fix it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem has nothing to do with
bash, or with the C++ code. To verify this, this trivial program has the same problem:And so does this Python program:
And you can try running under different shells and see that it makes no difference.
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.
So, this isn’t even really a programming question; it’s a TTY question. Which is why you can find a dup over at superuser.com instead of here. See the discussions there on the various ways to deal with this.
However, if you want to deal with it from within your program, rather than globally, you can check whether
stdinis atty, and if so usetermiosfunctions to temporarily turn off line mode. For example, something like this: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
man termiosto understand what you’re doing and what other effects it will have (and whether you want to do anything else).