stdin

Python Read from Stdin with Arguments

喜夏-厌秋 提交于 2019-11-30 19:52:38
问题 I want to read from python stdin but also to have input options in my program. When I try to pass an option to my programm I get the error file not found and my arguments are discarded. For parsing the arguments I use the following code: parser=argparse.ArgumentParser(description='Training and Testing Framework') parser.add_argument('--text', dest='text', help='The text model',required=True) parser.add_argument('--features', dest='features', help='The features model',required=True) parser.add

using QTextStream to read stdin in a non-blocking fashion

♀尐吖头ヾ 提交于 2019-11-30 19:21:37
Using Qt, I'm attempting to read the contents of the stdin stream in a non-blocking fashion. I'm using the QSocketNotifier to alert me when the socket has recieved some new data. The setup for the notifier looks like this: QSocketNotifier *pNot = new QSocketNotifier(STDIN_FILENO, QSocketNotifier::Read, this); connect(pNot, SIGNAL(activated(int)), this, SLOT(onData())); pNot->setEnabled(true); The onData() slot looks like this: void CIPCListener::onData() { qDebug() << Q_FUNC_INFO; QTextStream stream(stdin, QIODevice::ReadOnly); QString str; forever { fd_set stdinfd; FD_ZERO( &stdinfd ); FD_SET

Flushing Perl STDIN buffer

本秂侑毒 提交于 2019-11-30 18:29:56
Is there any way to clear the STDIN buffer in Perl? A part of my program has lengthy output (enough time for someone to enter a few characters) and after that output I ask for input, but if characters were entered during the output, they are "tacked on" to whatever is entered at the input part. Here is an example of my problem: for(my $n = 0; $n < 70000; $n++){ print $n . "\n"; } chomp(my $input = <STDIN>); print $input . "\n"; The output would include any characters entered during the output from that for loop. How could I either disable STDIN or flush the STDIN buffer (or any other way to

Why do I have to press Ctrl+D twice to close stdin?

拈花ヽ惹草 提交于 2019-11-30 18:24:03
I have the following Python script that reads numbers and outputs an error if the input is not a number. import fileinput import sys for line in (txt.strip() for txt in fileinput.input()): if not line.isdigit(): sys.stderr.write("ERROR: not a number: %s\n" % line) If I get the input from stdin, I have to press Ctrl + D twice to end the program. Why? I only have to press Ctrl + D once when I run the Python interpreter by itself. bash $ python test.py 1 2 foo 4 5 <Ctrl+D> ERROR: not a number: foo <Ctrl+D> bash $ In Python 3, this was due to a bug in Python's standard I/O library . The bug was

C select() timeout STDIN single char (no ENTER)

微笑、不失礼 提交于 2019-11-30 17:43:41
问题 I want to be able to use select() to work with entering a single char (no ENTER) from STDIN. So, when a user press a single key, select() should return immediately, not waiting for the user to hit ENTER. int main(void) { fd_set rfds; struct timeval tv; int retval; /* Watch stdin (fd 0) to see when it has input. */ FD_ZERO(&rfds); FD_SET(0, &rfds); /* Wait up to 2 seconds. */ tv.tv_sec = 2; tv.tv_usec = 0; retval = select(1, &rfds, NULL, NULL, &tv); if (retval == -1) perror("select()"); else

Redirecting STDIN, STDOUT, STDERR to /dev/null in C

拥有回忆 提交于 2019-11-30 17:40:02
In Stevens' UNIX Network Programming, he mentions redirecting stdin, stdout and stderr, which is needed when setting up a daemon. He does it with the following C code /* redirect stdin, stdout, and stderr to /dev/null */ open("/dev/null", O_RDONLY); open("/dev/null", O_RDWR); open("/dev/null", O_RDWR); I'm confused how these three 'know' they are redirecting the three std*. Especially since the last two commands are the same. Could someone explain or point me in the right direction? Presumably file descriptors 0, 1, and 2 have already been closed when this code executes, and there are no other

Flushing Perl STDIN buffer

耗尽温柔 提交于 2019-11-30 16:52:43
问题 Is there any way to clear the STDIN buffer in Perl? A part of my program has lengthy output (enough time for someone to enter a few characters) and after that output I ask for input, but if characters were entered during the output, they are "tacked on" to whatever is entered at the input part. Here is an example of my problem: for(my $n = 0; $n < 70000; $n++){ print $n . "\n"; } chomp(my $input = <STDIN>); print $input . "\n"; The output would include any characters entered during the output

Redirecting STDIN, STDOUT, STDERR to /dev/null in C

半世苍凉 提交于 2019-11-30 16:44:14
问题 In Stevens' UNIX Network Programming, he mentions redirecting stdin, stdout and stderr, which is needed when setting up a daemon. He does it with the following C code /* redirect stdin, stdout, and stderr to /dev/null */ open("/dev/null", O_RDONLY); open("/dev/null", O_RDWR); open("/dev/null", O_RDWR); I'm confused how these three 'know' they are redirecting the three std*. Especially since the last two commands are the same. Could someone explain or point me in the right direction? 回答1:

ncurses and stdin blocking

為{幸葍}努か 提交于 2019-11-30 16:04:28
I have stdin in a select() set and I want to take a string from stdin whenever the user types it and hits Enter . But select is triggering stdin as ready to read before Enter is hit, and, in rare cases, before anything is typed at all. This hangs my program on getstr() until I hit Enter . I tried setting nocbreak() and it's perfect really except that nothing gets echoed to the screen so I can't see what I'm typing. And setting echo() doesn't change that. I also tried using timeout(0) , but the results of that was even crazier and didn't work. What you need to do is tho check if a character is

Java+Eclipse: how do you debug a java program that is receiving piped/redirected stdin?

隐身守侯 提交于 2019-11-30 15:05:06
I'm using Eclipse to develop a Java program, and figured I'd add an option to my program to parse stdin if there are no arguments. (otherwise it parses a file) I am having problems if I execute "somecommand | java -jar myjar.jar" and went to debug... then realized I don't know how to start a process in Eclipse like that. And if I run it on the command prompt, I can't attach to a running process since the process starts immediately. Any suggestions on how to debug? edit : see, the thing is, I wrote my program originally to take a filename argument. Then I figured it would be useful for it to