stdout

Communicate with C program using stdin/out from Java

為{幸葍}努か 提交于 2021-02-05 11:34:12
问题 I want my Java program to communicate with a C program. This is just a simple example but I can't get it working. The Java program is supposed to run the C program and write to its input stream. The C program should see this and write to stdout in response. Finally, the Java program should read this response from the C program's stdout and print it to the screen. Running the C program from command line I get the desired behaviour. However, when ran from the Java program, it just "hangs" and

Why would I need use fflush on stdout before writing to stderr?

你离开我真会死。 提交于 2021-02-04 11:33:50
问题 I am reading 'UNIX Network Programming: The Sockets Networking API' and in the example code they have an error handling function which contains the following lines: fflush(stdout); /* in case stdout and stderr are the same */ fputs(buf, stderr); fflush(stderr); Where buf contains the error description. I don't understand why fflush is used on stdout on the first line and why the comment explains the reason for its use. 回答1: This is because of buffering. Stdout and stderr are usually buffered

Catch buffered stdout output of exec.Command

北城以北 提交于 2021-01-29 09:53:23
问题 I'm trying to catch output of external program. Example: #include <stdio.h> #include <unistd.h> #include <stddef.h> int main() { int i = 0; while(i < 10) { printf("i = %i\n", i++); usleep(2000000); } return 0; } And here is my main.go: package main import ( "bufio" "io" "log" "os/exec" ) func reecho(closer io.ReadCloser) { reader := bufio.NewReader(closer) for { s, e := reader.ReadString('\n') if e != nil { log.Println(e) break } log.Println(s) } } func main() { cmd := exec.Command(".

How to read single keystrokes without blocking the whole application?

给你一囗甜甜゛ 提交于 2021-01-29 05:47:01
问题 Because I didn't find a better way to read keystrokes on command line I'm currently using getch() . Unfortunately using getch() like this stops output on stdout : while True: handle_keystroke(getch()) Pressing buttons triggers handle_keystroke() and stdout is being printed in the terminal - line by line for each keystroke. Recommendations provided here didn't help. What do I do wrong? Btw: I do not need to use getch() . Is there a better way (e.g. using select() )? Update: (changed the title)

Check if Windows file is redirected to itself

China☆狼群 提交于 2021-01-28 12:34:59
问题 I'm trying to figure out how I can test if a file is being redirected to itself, e.g. .\command.exe file1 > file1 In the *nix world, I'd just use something like this: // Language agnostic... if (file_dev == out_dev && file_ino == out_ino) { printf("%s\n", "same file!"); } But in Windows, if I try to do this: // This (language) is Go... // create fileStat... // now stat stdout outStat, err := os.Stdout.Stat() // error check if os.SameFile(fileStat, outStat) { fmt.Println("same file!") } ...I

Check if Windows file is redirected to itself

安稳与你 提交于 2021-01-28 12:33:01
问题 I'm trying to figure out how I can test if a file is being redirected to itself, e.g. .\command.exe file1 > file1 In the *nix world, I'd just use something like this: // Language agnostic... if (file_dev == out_dev && file_ino == out_ino) { printf("%s\n", "same file!"); } But in Windows, if I try to do this: // This (language) is Go... // create fileStat... // now stat stdout outStat, err := os.Stdout.Stat() // error check if os.SameFile(fileStat, outStat) { fmt.Println("same file!") } ...I

bash stdout redirection in a for loop

强颜欢笑 提交于 2021-01-28 11:10:57
问题 Possible duplicate Hello, I'm struggling with output redirection within a bash for loop. I have several similar bash scripts, each scripts being launched on a different input file. The scripts called a tool with a few arguments. The bash scripts are like this : my_tool --input input_file --arg1 arg1_name --arg2 arg2_name > output_dir/result_X.dat X being a string which is unique for each script. I run all of them in a for loop : for script in scripts_dir/*.sh do bash $script done However, the

bash stdout redirection in a for loop

走远了吗. 提交于 2021-01-28 11:10:13
问题 Possible duplicate Hello, I'm struggling with output redirection within a bash for loop. I have several similar bash scripts, each scripts being launched on a different input file. The scripts called a tool with a few arguments. The bash scripts are like this : my_tool --input input_file --arg1 arg1_name --arg2 arg2_name > output_dir/result_X.dat X being a string which is unique for each script. I run all of them in a for loop : for script in scripts_dir/*.sh do bash $script done However, the

Cython using gmp arithmetic

夙愿已清 提交于 2021-01-28 09:15:27
问题 I'm trying to implement a simple code in cython using Jupyter notebook (I use python 2) and using gmp arithmetic in order to handle very large integers. I'm not a gmp/cython expert. My question is : how do I print the value a in the function fib(). The following code returns {}. As fas as I can understand it has to do with stdout. For instance I tried gmp_printf and it didn't work. %%cython --link-args=-lgmp cdef extern from "gmp.h": ctypedef struct mpz_t: pass cdef void mpz_init(mpz_t) cdef

Why is the STDOUT line printed after the STDERR line?

て烟熏妆下的殇ゞ 提交于 2021-01-27 07:22:23
问题 I completly don't understand this behaviour. I have a very simple Perl script: #!/usr/bin/perl use strict; use warnings; print "line 1\n"; print STDERR "line 2\n"; If I run it from console I will get what I expect: $ perl a.pl line 1 line 2 But if I redirect it to file, I will get the lines in the reverse order: $ perl a.pl &> a $ cat a line 2 line 1 How can I capture all the outputs STDOUT+STDERR to the file with the same order I get in the console? 回答1: This is an effect of buffering. When