stdin

Read from file or stdin

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 07:15:12
问题 I am writing a utility which accepts either a filename, or reads from stdin. I would like to know the most robust / fastest way of checking to see if stdin exists (data is being piped to the program) and if so reading that data in. If it doesn't exist, the processing will take place on the filename given. I have tried using the following the test for size of stdin but I believe since it's a stream and not an actual file, it's not working as I suspected it would and it's always printing -1 . I

What is the difference between STDIN and $stdin in Ruby?

断了今生、忘了曾经 提交于 2019-12-17 06:33:56
问题 Ruby has two ways of referring to the standard input: The STDIN constant , and the $stdin global variable. Aside from the fact that I can assign a different IO object to $stdin because it's not a constant (e.g. before forking to redirect IO in my children), what's the difference between STDIN and $stdin ? When should I use each in my code? If I reassign $stdin , does it affect STDIN ? And does this also apply to STDOUT / $stdout and STDER / $stderr ? 回答1: If $stdin is reassigned, STDIN is not

Interactive input/output using python

瘦欲@ 提交于 2019-12-17 05:54:09
问题 I have a program that interacts with the user (acts like a shell), and I want to run it using python subprocess module interactively. That means, I want the possibility to write to stdin and immediately get the output from stdout. I tried many solutions offered here, but none of them seems to work for my needs. The code I've written based on Running an interactive command from within python import Queue import threading import subprocess def enqueue_output(out, queue): for line in iter(out

Read stdin stream in a batch file

故事扮演 提交于 2019-12-17 04:28:58
问题 Is it possible to use a piped stdin stream inside a batch file? I want to be able to redirect the output of one command into my batch file process.bat list so: C:\>someOtherProgram.exe | process.bat My first attempt looked like: echo OFF setlocal :again set /p inputLine="" echo.%inputLine% if not (%inputLine%)==() goto again endlocal :End When I test it with type testFile.txt | process.bat it prints out the first line repeatedly. Is there another way? 回答1: set /p doesn't work with pipes, it

Best practices with STDIN in Ruby?

爷,独闯天下 提交于 2019-12-17 02:26:15
问题 I want to deal with the command line input in Ruby: > cat input.txt | myprog.rb > myprog.rb < input.txt > myprog.rb arg1 arg2 arg3 ... What is the best way to do it? In particular I want to deal with blank STDIN, and I hope for an elegant solution. #!/usr/bin/env ruby STDIN.read.split("\n").each do |a| puts a end ARGV.each do |b| puts b end 回答1: Following are some things I found in my collection of obscure Ruby. So, in Ruby, a simple no-bells implementation of the Unix command cat would be: #

How to detect if Console.In (stdin) has been redirected?

匆匆过客 提交于 2019-12-16 22:26:31
问题 I want to write a console application that have a different behavior depending if the input is coming from keyboard or from, say, a file. Is it possible? What's the most elegant way to do it in C#? 回答1: You can find out by p/invoking the Windows FileType() API function. Here's a helper class: using System; using System.Runtime.InteropServices; public static class ConsoleEx { public static bool IsOutputRedirected { get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stdout)); } }

c wait for stdin read?

删除回忆录丶 提交于 2019-12-14 04:26:22
问题 in my application, i'm trying to achieve something like this: i have: data 0, data 1, data 2, ... data n. some parameters to pass flow: starts the program with some parameters and write data 0 into stdin the program does calculation according to the passed data "data 0" and parameters "wait" for new stdin and (clear old stdin,buffer and variables?) repeat 1 ~2 when i put data 1, data 2... and so on when reaches data n, terminate(or if i input a abort code to stdin telling the program to

Reading from text file or stdin

喜夏-厌秋 提交于 2019-12-14 03:47:34
问题 I have a program that basically reads a text file and counts the number of occurrences of each word on each line. Everything works properly when reading from a text file using an ifstream, however, if a file name is not entered on the command line, I need to read from stdin instead. I use the following to open and read in the file currently: map<string, map<int,int>,compare> tokens; ifstream text; string line; int count = 1; if (argc > 1){ try{ text.open(argv[1]); } catch (runtime_error& x){

Can you open stdin as a file on MS Windows in Python?

ぐ巨炮叔叔 提交于 2019-12-14 03:47:23
问题 On Linux, I'm using supbprocess.Popen to run an app. The command line of that app requires a path to an input file. I learned I can pass the path /dev/stdin to the command line, and then use Python's subproc.stdin.write() to send input to the subprocess. import subprocess kw['shell'] = False kw['executable'] = '/path/to/myapp' kw['stdin'] = subprocess.PIPE kw['stdout'] = subprocess.PIPE kw['stderr'] = subprocess.PIPE subproc = subprocess.Popen(['','-i','/dev/stdin'],**kw) inbuff = [u'my lines

Custom standard input for python subprocess

馋奶兔 提交于 2019-12-14 03:42:23
问题 I'm running an SSH process like this: sshproc = subprocess.Popen([command], shell=True) exit = os.waitpid(sshproc.pid, 0)[1] This works and opens an interactive terminal. Based on the documentation for subprocess , sshproc is using the script's sys.stdin . The question is: how can I print to stderr or a file what input is being received to this child process? I am creating a logging API, and currently lose the ability to record what commands are run over this SSH session. I don't need the