0

I’m using

/dev/tty
in a program to enable interactive user input after reading some data, that has been piped into the program. This does not work on Windows, because the
/dev/tty
pseudo-file does not exist there. Is there any equivalent pseudo file or an alternative way to read terminal input, when standard input of a program is not the terminal on Windows?

Example program

To clarify the issue, here is a simple demo program, written in Go:

			
package main

import (
    "fmt"
    "io"
    "os"
)

func main() {
    fmt.Println("Received via standard input:")
    io.Copy(os.Stdout, os.Stdin)

    tty, err := os.Open("/dev/tty")
    if err != nil {
        panic(err)
    }
    defer tty.Close()
    var interactiveInput string
    fmt.Print("Enter some intput interactively: ")
    fmt.Fscanln(tty, &interactiveInput)
    fmt.Println("Received interactively:", interactiveInput)
}

On a UNIXy system I get this behaviour:

$ echo foo | ./tty_demo
Received via standard input:
foo
Enter some intput interactively: bar
Received interactively: bar

On Windows I get this behaviour:

> echo foo | tty_demo.exe
Received via standard input:
foo
panic: open /dev/tty: The system cannot find the path specified.

goroutine 1 [running]:
main.main()
        /tmp/tty_demo.go:15 +0x305
Anonymous Asked question May 13, 2021