stdin

In python, how to check the end of standard input streams (sys.stdin) and do something special on that

有些话、适合烂在心里 提交于 2019-12-03 11:22:32
I want to do something like: for line in sys.stdin: do_something() if is **END OF StdIn**: do_something_special() After a few tries, for now I am doing this: while True: try: line = sys.stdin.next() print line, except StopIteration: print 'EOF!' break Or with this: while True: line = sys.stdin.readline() if not line: print 'EOF!' break print line, I think both above ways are very similar. I want to know is there a more elegant (pythonic) way to do this? Early failed tries: I first tried to catch the StopIteration from inside or outside of a for loop, but I soon realize that since the

sys.stdin.readline() reads without prompt, returning 'nothing in between'

守給你的承諾、 提交于 2019-12-03 11:16:21
I have a function that executes the following (among other things): userinput = stdin.readline() betAmount = int(userinput) Is supposed to take input integer from stdin as a string and convert it to an integer. When I call the function, however, it returns a single newline character (it doesn't even wait for me to input anything). Earlier in the program I get some input in the form of: stdin.read(1) to capture a single character. Could this have something to do with it? Am I somehow writing a newline character to the next line of stdin? How can I fix this? stdin.read(1) reads one character

If fclose(0) is called, does this close stdin?

旧时模样 提交于 2019-12-03 11:08:36
If fclose(0) is called, does this close stdin? The reason why I'm asking this is that for some reason, stdin is being closed in my application and I cannot figure out why. I checked for fclose (stdin) and this is not in the application and so I was wondering if fclose(0) could cause undefined behaviour such as closing stdin? If not, what are other ways that stdin could be erroneously closed? The signature of fclose is this: int fclose ( FILE * stream ); That means, fclose expects a pointer to FILE object. So if you pass 0 , instead of a pointer, 0 would be understood as NULL pointer 1 . If its

Cannot trigger 'end' event using CTRL D when reading from stdin

倾然丶 夕夏残阳落幕 提交于 2019-12-03 10:42:29
In the following code process.stdin.resume(); process.stdin.setEncoding('utf8'); process.stdin.on('data', function(chunk) { process.stdout.write('data: ' + chunk); }); process.stdin.on('end', function() { process.stdout.write('end'); }); i can't trigger the 'end' event using ctrl+D, and ctrl+C just exit without triggering it. hello data: hello data data: data foo data: foo ^F data: ♠ ^N data: ♫ ^D data: ♦ ^D^D data: ♦♦ I'd change this: process.stdin.on('end', function() { process.stdout.write('end'); }); To this: process.on('SIGINT', function(){ process.stdout.write('\n end \n'); process.exit(

Checking Standard Input in C#

南笙酒味 提交于 2019-12-03 09:03:06
问题 I'm writing a small command line utility whose purpose is to parse the output of another utility. I want it to be invokable in two ways: c:\> myutility filewithoutput.txt Or, c:\> otherutility -args | myutility So, basically, standard in or a file argument. My first attempt at this looked like this: TextReader reader; if (args.Length > 1) { reader = new StreamReader(new FileStream(args[1], FileMode.Open)); } else { reader = Console.In; } Process(reader); The file argument works fine, and

ffmpeg: which file formats support stdin usage?

强颜欢笑 提交于 2019-12-03 08:44:20
问题 I know ffmpeg is able to read data from stdin rather than reading from disk using ffmpeg -i - . Is this supported for all file formats? If it is not, is there a list which file formats are supported? 回答1: You need to run ffmpeg -protocols to determine if the pipe protocol (the read and write from stdin and stdout) supported in your version of ffmpeg and then ffmpeg -formats to see the list of supported formats. In the excerpt below you will see the note on output pipe that it must be seekable

How do I iterate over all lines of files passed on the command line?

自闭症网瘾萝莉.ら 提交于 2019-12-03 08:40:45
问题 I usually do this in Perl: whatever.pl while(<>) { #do whatever; } then cat foo.txt | whatever.pl Now, I want to do this in Python. I tried sys.stdin but I have no idea how to do as I have done in Perl. How can I read the input? 回答1: Try this: import fileinput for line in fileinput.input(): process(line) 回答2: import sys def main(): for line in sys.stdin: print line if __name__=='__main__': sys.exit(main()) 回答3: Something like this: import sys for line in sys.stdin: # whatever 回答4: import sys

How to read non-ASCII characters from CLI standard input

别说谁变了你拦得住时间么 提交于 2019-12-03 07:11:43
问题 If I type å in CMD, fgets stop waiting for more input and the loop runs until I press ctrl-c . If I type a "normal" characters like a-z0-9!?() it works as expected. I run the code in CMD under Windows 7 with UTF-8 as charset ( chcp 65001 ), the file is saved as UTF-8 without bom. I use PHP 5.3.5 (cli). <?php echo "ÅÄÖåäö work here.\n"; while(1) { echo '> '. fgets(STDIN); } ?> If I change charset to chcp 1252 the loop doesn't break when I type å and it print "> å" but the "ÅÄÖåäö work here"

How to stop reading multiple lines from stdin using Scanner?

与世无争的帅哥 提交于 2019-12-03 06:59:31
I'm working on an JAVA assignment should process multiple lines of input. The instructions read "Input is read from stdin." An example of sample input is given: one 1 two 2 three 3 I don't understand what the above sample input "read from stdin" means. Here's a test program I wrote that isolates my confusion: import java.io.*; import java.util.Scanner; class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); while(stdin.hasNextLine()) { String line = stdin.nextLine(); String[] tokens = line.split(" "); System.out.println(Integer.parseInt(tokens[1])); } }

Read from pipe line by line in C

社会主义新天地 提交于 2019-12-03 06:53:13
How can I separate the lines which are coming from a pipe. In the pipe there is this text: HALLO:500\n TEST:300\N ADAD ADAWFFA AFAGAGAEG I want to separate the lines from the pipe because i want to save the values in variables. Here is my c code: #include <stdio.h> #include <stdlib.h> #define BUFFERSIZE 1 int main(int argc, char **argv){ unsigned char buffer[BUFFERSIZE]; FILE *instream; int bytes_read=0; int buffer_size=0; buffer_size=sizeof(unsigned char)*BUFFERSIZE; /* open stdin for reading */ instream=fopen("/dev/stdin","r"); /* did it open? */ if(instream!=NULL){ /* read from stdin until