stdin

Determine if Stdin has data with Go

天大地大妈咪最大 提交于 2019-12-05 05:01:22
Is there a way to check if the input stream ( os.Stdin ) has data? The post Read from initial stdin in GO? shows how to read the data, but unfortunately blocks if no data is piped into the stdin. os.Stdin is like any other "file", so you can check it's size: package main import ( "fmt" "os" ) func main() { file := os.Stdin fi, err := file.Stat() if err != nil { fmt.Println("file.Stat()", err) } size := fi.Size() if size > 0 { fmt.Printf("%v bytes available in Stdin\n", size) } else { fmt.Println("Stdin is empty") } } I built this as a "pipe" executable, here is how it works: $ ./pipe Stdin is

How do I check if my program has data piped into it

这一生的挚爱 提交于 2019-12-05 04:15:49
Im writing a program that should read input via stdin, so I have the following contruct. FILE *fp=stdin; But this just hangs if the user hasn't piped anything into the program, how can I check if the user is actually piping data into my program like gunzip -c file.gz |./a.out #should work ./a.out #should exit program with nice msg. thanks Since you're using file pointers, you'll need both isatty() and fileno() to do this: #include <unistd.h> #include <stdio.h> int main(int argc, char* argv[]) { FILE* fp = stdin; if(isatty(fileno(fp))) { fprintf(stderr, "A nice msg.\n"); exit(1); } /* carry on.

Golang: Child Processes become Zombies

你。 提交于 2019-12-05 03:50:59
I have an application in Go that reroutes the STDIN and STDOUT of binaries and then runs them. In a nutshell I'm doing: - create command object with the binary path (lets call the object command A) - create command object with the binary path (calling it command B) - set the stdout of command B to the stdin of Command A - start command A - start command B I noticed whenever the process for command B exits while command A is running, it becomes a zombie process in the process table. Here's an example: commandA := exec.Command("samplebin") commandB := exec.Command("sample2bin") cmdAStdin :=

using stdin in pycharm [duplicate]

微笑、不失礼 提交于 2019-12-05 03:46:47
This question already has answers here : Reading from a file with sys.stdin in Pycharm (5 answers) Closed 2 years ago . I have started to use pycharm and i can't seem to in the command line i use to run the program in this way: python parse_file.py < test.txt when parse_file.py can be simple as : import sys for line in sys.stdin: print line i cant find in the configuration where to do it i tried typeing something like <test.txt in the script parameters but no luck I have looked at https://www.jetbrains.com/pycharm/help/run-debug-configuration-python.html but no luck there also Thanks PyCharm's

Unknown method process.openStdin()

江枫思渺然 提交于 2019-12-05 03:06:14
I'm trying to pipe grep results into nodejs script. I've found, that I should receive data from process.stdin. Also I've found several ways to work with stdin. But they are different and I can't find all information about it. I know four ways (first 3 start with var data = "" ): 1) Most popular in search results process.stdin.resume(); process.stdin.setEncoding( 'utf8' ); process.stdin.on('data', function(chunk) { data += chunk; }); process.stdin.on('end', function() { console.log('data: ' + data); }); 2) Looks like the first one, but with unknown function process.openStdin() var stdin =

Python read from command line arguments or stdin

China☆狼群 提交于 2019-12-05 02:37:37
When writing text-oriented command line programs in Python, I often want to read either all the files passed on the command line, or (XOR) standard input (like Unix cat does, or Perl's <> ). So, I say if len(args) == 0: # result from optparse input = sys.stdin else: input = itertools.chain(*(open(a) for a in args)) Is this the Pythonic way of doing this, or did my miss some part of the library? eumiro You need fileinput . A standard use case is: import fileinput for line in fileinput.input(): process(line) Andreas Jung See How do you read from stdin in Python? Michael Scheper In Python 3,

Passing data between Python and C# without writing a file

笑着哭i 提交于 2019-12-05 01:58:49
问题 I would like to pass binary information between Python and C#. I would assume that you can open a standard in/out channel and read and write to that like a file, but there are a lot of moving parts, and I don't know C# too well. I want to do this sort of thing, but without writing a file. # python code with open(DATA_PIPE_FILE_PATH, 'wb') as fid: fid.write(blob) subprocess.Popen(C_SHARP_EXECUTABLE_FILE_PATH) with open(DATA_PIPE_FILE_PATH, 'rb') as fid: 'Do stuff with the data' // C# code

how use EOF stdin in C

喜欢而已 提交于 2019-12-05 01:14:42
问题 I need to input coordinates into an array until EOF is encountered, but something is wrong in my code. I used ctrl+Z, ctrl+D int main() { int x[1000],y[1000]; int n=0,nr=0,a,b,i; printf("Enter the coordinates:\n"); while(scanf ( "%d %d ", &a, &b) == 2) { x[n]=a; y[n]=b; n++; } if (!feof(stdin)) { printf("Wrong\n"); } else { for(i=0;i<n;i++) printf("%d %d\n", x[i], y[i]); } return 0; } 回答1: I suggest using while(!feof(stdin) && scanf ( "%d %d ", &a, &b) == 2) and actually it is better to test

How to change emscripten browser input method from window.prompt to something more sensible?

≯℡__Kan透↙ 提交于 2019-12-05 00:59:14
问题 I have a C++ function which once called consumes input from stdin. Exporting this function to javascript using emscripten causes calls to window.prompt. Interacting with browser prompt is really tedious task. First of all you can paste only one line at time. Secondly the only way to indicate EOF is by pressing 'cancel'. Last but not least the only way (in case of my function) to make it stop asking user for input by window.prompt is by checking the checkbox preventing more prompts to pop up.

How to print stdout immediately?

一个人想着一个人 提交于 2019-12-05 00:02:24
How can I immediately output stdout ? stdout is going to print after all input is complete. require 'open3' def run(cmd) Open3.popen3(cmd) do |stdin, stdout, stderr, thread| Thread.new do stdout.each {|l| puts l} end Thread.new do while thread.alive? stdin.puts $stdin.gets end end thread.join end end run ("ruby file_to_test.rb") file_to_test.rb: puts "please, enter s" puts "please, enter q" s = gets.chomp! q = gets.chomp! puts s puts q The result after running main.rb is: somestring somestring2 please, enter s please, enter q somestring somestring2 How can I immediately output stdout ? Ruby is