stdin

How can I “intercept” Ctrl+C in a CLI application?

冷暖自知 提交于 2019-11-26 01:15:08
问题 How can I intercept Ctrl + C (which normally would kill the process) in a CLI (command line interface) Java application? Does a multi-platform solution exist (Linux, Solaris, Windows)? I\'m using Console \'s readLine() , but if necessary, I could use some other method to read characters from standard input. 回答1: Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { /* my shutdown code here */ } }); This should be able to intercept the signal, but only as an intermediate step

Detect if stdin is a terminal or pipe?

白昼怎懂夜的黑 提交于 2019-11-26 00:47:18
问题 When I execute \" python \" from the terminal with no arguments it brings up the Python interactive shell. When I execute \" cat | python \" from the terminal it doesn\'t launch the interactive mode. Somehow, without getting any input, it has detected that it is connected to a pipe. How would I do a similar detection in C or C++ or Qt? 回答1: Use isatty : #include <stdio.h> #include <io.h> ... if (isatty(fileno(stdin))) printf( "stdin is a terminal\n" ); else printf( "stdin is a file or a pipe

How to read a line from the console in C?

故事扮演 提交于 2019-11-26 00:28:24
问题 What is the simplest way to read a full line in a C console program The text entered might have a variable length and we can\'t make any assumption about its content. 回答1: You need dynamic memory management, and use the fgets function to read your line. However, there seems to be no way to see how many characters it read. So you use fgetc: char * getline(void) { char * line = malloc(100), * linep = line; size_t lenmax = 100, len = lenmax; int c; if(line == NULL) return NULL; for(;;) { c =

I am not able to flush stdin

人盡茶涼 提交于 2019-11-26 00:22:49
问题 How to flush the stdin ?? Why is it not working in the following code snippet? #include <string.h> #include <stdio.h> #include <malloc.h> #include <fcntl.h> int main() { int i=0,j=0, sat; char arg[256]; char * argq; argq = malloc(sizeof(char)*10); printf(\"Input the line\\n\"); i=read(0, arg, sizeof(char)*9); arg[i-1]=\'\\0\'; fflush(stdin); i=read(0, argq, sizeof(char)*5); argq[i-1]=\'\\0\'; puts(arg); puts(argq); return 0; } Now if i give the input as 11 characters, only 9 should be read

How to trick an application into thinking its stdout is a terminal, not a pipe

假如想象 提交于 2019-11-26 00:21:52
问题 I\'m trying to do the opposite of \"Detect if stdin is a terminal or pipe?\". I\'m running an application that\'s changing its output format because it detects a pipe on STDOUT, and I want it to think that it\'s an interactive terminal so that I get the same output when redirecting. I was thinking that wrapping it in an expect script or using a proc_open() in PHP would do it, but it doesn\'t. Any ideas out there? 回答1: Aha! The script command does what we want... script --return --quiet -c "

Python - How do I pass a string into subprocess.Popen (using the stdin argument)?

女生的网名这么多〃 提交于 2019-11-25 22:01:14
问题 If I do the following: import subprocess from cStringIO import StringIO subprocess.Popen([\'grep\',\'f\'],stdout=subprocess.PIPE,stdin=StringIO(\'one\\ntwo\\nthree\\nfour\\nfive\\nsix\\n\')).communicate()[0] I get: Traceback (most recent call last): File \"<stdin>\", line 1, in ? File \"/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py\", line 533, in __init__ (p2cread, p2cwrite, File \"/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py\", line 830, in _get_handles

How do I read a string entered by the user in C?

走远了吗. 提交于 2019-11-25 21:48:53
问题 I want to read the name entered by my user using C programmes. For this I wrote: char name[20]; printf(\"Enter name: \"); gets(name); But using gets is not good, so what is a better way? 回答1: You should never use gets (or scanf with an unbounded string size) since that opens you up to buffer overflows. Use the fgets with a stdin handle since it allows you to limit the data that will be placed in your buffer. Here's a little snippet I use for line input from the user: #include <stdio.h>

How do you read from stdin?

假装没事ソ 提交于 2019-11-25 21:47:06
问题 I\'m trying to do some of the code golf challenges, but they all require the input to be taken from stdin . How do I get that in Python? 回答1: You could use the fileinput module: import fileinput for line in fileinput.input(): pass fileinput will loop through all the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided. Note: line will contain a trailing newline; to remove it use line.rstrip() 回答2: There's a few ways to

Using fflush(stdin)

眉间皱痕 提交于 2019-11-25 21:35:17
问题 So a quick Google search for fflush(stdin) for clearing the input buffer reveals numerous websites warning against using it. And yet that\'s exactly how my CS professor taught the class to do it. How bad is using fflush(stdin) ? Should I really abstain from using it, even though my professor is using it and it seems to work flawlessly? 回答1: Simple: this is undefined behavior, since fflush is meant to be called on an output stream. This is an excerpt from the C standard: int fflush(FILE