Questions tagged [stdin]

Standard input (stdin, file descriptor 0) is the input stream to a program.

3861 questions
114
votes
19 answers

How can I clear an input buffer in C?

I have the following program: int main(int argc, char *argv[]) { char ch1, ch2; printf("Input the first character:"); // Line 1 scanf("%c", &ch1); printf("Input the second character:"); // Line 2 ch2 = getchar(); …
ipkiss
  • 13,311
  • 33
  • 88
  • 123
108
votes
6 answers

How do I write to a Python subprocess' stdin?

I'm trying to write a Python script that starts a subprocess, and writes to the subprocess stdin. I'd also like to be able to determine an action to be taken if the subprocess crashes. The process I'm trying to start is a program called nuke which…
jonathan topf
  • 7,897
  • 17
  • 55
  • 85
101
votes
7 answers

Using fflush(stdin)

So a quick Google search for fflush(stdin) for clearing the input buffer reveals numerous websites warning against using it. And yet that's exactly how my CS professor taught the class to do it. How bad is using fflush(stdin)? Should I really…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
96
votes
7 answers

Reading an integer from standard input

How do I use the fmt.Scanf function in Go to get an integer input from the standard input? If this can't be done using fmt.Scanf, what's the best way to read a single integer?
yasith
  • 8,981
  • 7
  • 27
  • 32
96
votes
5 answers

Redirecting standard input\output in Windows PowerShell

What is the required syntax to redirect standard input/output on Windows PowerShell? On Unix, we use: $./program output.txt How do I execute the same task in PowerShell?
Nitin
  • 2,624
  • 7
  • 29
  • 43
91
votes
6 answers

Continuously read from STDOUT of external process in Ruby

I want to run blender from the command line through a ruby script, which will then process the output given by blender line by line to update a progress bar in a GUI. It's not really important that blender is the external process whose stdout I need…
ehsanul
  • 7,737
  • 7
  • 33
  • 43
90
votes
4 answers

How do file descriptors work?

Can someone tell me why this does not work? I'm playing around with file descriptors, but feel a little lost. #!/bin/bash echo "This" echo "is" >&2 echo "a" >&3 echo "test." >&4 The first three lines run fine, but the last two error out. Why?
Trcx
  • 4,164
  • 6
  • 30
  • 30
87
votes
7 answers

How do I check if stdin has some data?

In Python, how do you check if sys.stdin has data or not? I found that os.isatty(0) can not only check if stdin is connected to a TTY device, but also if there is data available. But if someone uses code such as sys.stdin =…
mlzboy
  • 14,343
  • 23
  • 76
  • 97
87
votes
4 answers

How can I "intercept" Ctrl+C in a CLI application?

How can I intercept Ctrl+C (which normally would kill the process) in a CLI (command line interface) Java application? Does a multi-platform solution exist (Linux, Solaris, Windows)? I'm using Console's readLine(), but if necessary, I could use some…
83
votes
3 answers

Optional stdin in Python with argparse

I found the very useful syntax parser.add_argument('-i', '--input-file', type=argparse.FileType('r'), default='-') for specifying an input file or using stdin—both of which I want in my program. However, the input file is not always required. If…
Justin Force
  • 6,203
  • 5
  • 29
  • 39
82
votes
9 answers

Read from File, or STDIN

I've written a command line utility that uses getopt for parsing arguments given on the command line. I would also like to have a filename be an optional argument, such as it is in other utilities like grep, cut etc. So, I would like it to have the…
Ryan R. Rosario
  • 5,114
  • 9
  • 41
  • 56
81
votes
8 answers

Programmatically read from STDIN or input file in Perl

What is the slickest way to programatically read from stdin or an input file (if provided) in Perl?
syker
  • 10,912
  • 16
  • 56
  • 68
78
votes
7 answers

How to read mutliline input from stdin into variable and how to print one out in shell(sh,bash)?

What I want to do is the following: read in multiple line input from stdin into variable A make various operations on A pipe A without losing delimiter symbols (\n,\r,\t,etc) to another command The current problem is that, I can't read it in with…
kakas
75
votes
8 answers

Reading console input in Kotlin

I am attempting to accept input from the console in Kotlin but it is difficult because I am not too sure about the syntax. I begin with the main fun main(args: Array) { } WHAT should I enter after this? I am aware that the println() and…
siur
  • 919
  • 2
  • 9
  • 11
73
votes
5 answers

Read all text from stdin to a string

I'm writing a program in Node.js that (in some situations) wants to act as a simple filter: read everything from stdin (up to end of file), do some processing, write the result to stdout. How do you do the 'read everything from stdin' part? The…
rwallace
  • 31,405
  • 40
  • 123
  • 242