Redirect stdin and stdout in Java

You’ve attempted to write to the output stream before you attempt to listen on the input stream, so it makes sense that you’re seeing nothing. For this to succeed, you will need to use separate threads for your two streams. i.e., import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Scanner; public class … Read more

Reading console input in Kotlin

Note that since Kotlin 1.6 readLine()!! should be replaced with readln(). Here are A+B examples in Kotlin reading from stdin: fun main() { val (a, b) = readLine()!!.split(‘ ‘) println(a.toInt() + b.toInt()) } or fun main(vararg args: String) { val (a, b) = readLine()!!.split(‘ ‘).map(String::toInt) println(a + b) } or fun readInts() = readLine()!!.split(‘ ‘).map … Read more

Read from File, or STDIN

The fileinput module may do what you want – assuming the non-option arguments are in args then: import fileinput for line in fileinput.input(args): print line If args is empty then fileinput.input() will read from stdin; otherwise it reads from each file in turn, in a similar manner to Perl’s while(<>).

PHP CLI: How to read a single character of input from the TTY (without waiting for the enter key)?

The solution for me was to set -icanon mode on the TTY (using stty). Eg.: stty -icanon So, the the code that now works is: #!/usr/bin/php <?php system(“stty -icanon”); echo “input# “; while ($c = fread(STDIN, 1)) { echo “Read from STDIN: ” . $c . “\ninput# “; } ?> Output: input# fRead from STDIN: … Read more

getpasswd functionality in Go?

The following is one of best ways to get it done. First get term package by go get golang.org/x/term package main import ( “bufio” “fmt” “os” “strings” “syscall” “golang.org/x/term” ) func main() { username, password, _ := credentials() fmt.Printf(“Username: %s, Password: %s\n”, username, password) } func credentials() (string, string, error) { reader := bufio.NewReader(os.Stdin) fmt.Print(“Enter … Read more

Is there any way to peek at the stdin buffer?

Portably, you can get the next character in the input stream with getchar() and then push it back with ungetc(), which results in a state as if the character wasn’t removed from the stream. The ungetc function pushes the character specified by c (converted to an unsigned char) back onto the input stream pointed to … Read more