stdin

Is there any way to peek at the stdin buffer?

谁说我不能喝 提交于 2019-11-27 01:57:49
We know that stdin is, by default, a buffered input; the proof of that is in usage of any of the mechanisms that "leave data" on stdin , such as scanf() : int main() { char c[10] = {'\0'}; scanf("%9s", c); printf("%s, and left is: %d\n", c, getchar()); return 0; } ./a.out hello hello, and left is 10 10 being newline of course... I've always been curious, is there any way to "peek" at the stdin buffer without removing whatever may reside there? EDIT A better example might be: scanf("%9[^.]", c); With an input of "at.ct", now I have "data" ( ct\n ) left on stdin , not just a newline. Portably,

Read a character from standard input in Go (without pressing Enter)

ε祈祈猫儿з 提交于 2019-11-27 01:51:32
问题 I want to my app shows: press any key to exit ... And when I pressed any key, it exits. How can I achieve this? Note: I have googled but all of what I've found needed to press enter at the end. I want something like Console.ReadKey() in C#. I am running MS Windows. 回答1: termbox-go is a light-weight Go-native package which offers some rudimentary terminal control. Including the ability to get input in raw mode (read one character at a time without the default line-buffered behaviour). It also

Is there any way to pass 'stdin' as an argument to another process in python?

只谈情不闲聊 提交于 2019-11-27 01:37:23
问题 I'm trying to create a script which is using multiprocessing module with python. The script (lets call it myscript.py) will get the input from another script with pipe. Assume that I call the scripts like this; $ python writer.py | python myscript.py And here is the codes; // writer.py import time, sys def main(): while True: print "test" sys.stdout.flush() time.sleep(1) main() //myscript.py def get_input(): while True: text = sys.stdin.readline() print "hello " + text time.sleep(3) if __name

launch an exe/process with stdin stdout and stderr?

旧城冷巷雨未停 提交于 2019-11-27 01:30:31
With C++ how do i launch an exe/process with stdin stdout and stderr? I know how to do this in .NET and i remember using popen in the past but popen seems to allow stdin OR stdout not both and not all 3. I need this for windows but a linux solution is welcome as i'll need it for the same project in the future. You shoud use CreateProcess from WinApi. It takes as argument an object of struct STARTUP_INFO type. You can set hStdin, hStdout, and hStderr fields of the object to redirect those streams of child process to file handles you want (file, pipe, socket...) MSalters A portable solution

How do you read from stdin in python from a pipe which has no ending

馋奶兔 提交于 2019-11-27 01:19:01
问题 I've problem to read from Standard input or pipe in python when the pipe is from a "open" (do not know right name) file. I have as example pipetest.py: import sys import time k = 0 try: for line in sys.stdin: k = k + 1 print line except KeyboardInterrupt: sys.stdout.flush() pass print k I run a program that have continues output and Ctrl+c after a while $ ping 127.0.0.1 | python pipetest.py ^C0 I get no output. But if I go via an ordinary file it works. $ ping 127.0.0.1 > testfile.txt this is

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

落爺英雄遲暮 提交于 2019-11-27 00:57:59
I want to read a single character at-a-time from the command line in PHP, however it seems as though there is some kind of input buffering from somewhere preventing this. Consider this code: #!/usr/bin/php <?php echo "input# "; while ($c = fread(STDIN, 1)) { echo "Read from STDIN: " . $c . "\ninput# "; } ?> Typing in "foo" as the input (and pressing enter), the output I am getting is: input# foo Read from STDIN: f input# Read from STDIN: o input# Read from STDIN: o input# Read from STDIN: input# The output I am expecting is: input# f input# Read from STDIN: f input# o input# Read from STDIN: o

nodejs how to read keystrokes from stdin

家住魔仙堡 提交于 2019-11-26 23:35:25
Is it possible to listen for incoming keystrokes in a running nodejs script? If I use process.openStdin() and listen to its 'data' event then the input is buffered until the next newline, like so: // stdin_test.js var stdin = process.openStdin(); stdin.on('data', function(chunk) { console.log("Got chunk: " + chunk); }); Running this, I get: $ node stdin_test.js <-- type '1' <-- type '2' <-- hit enter Got chunk: 12 What I'd like is to see: $ node stdin_test.js <-- type '1' (without hitting enter yet) Got chunk: 1 I'm looking for a nodejs equivalent to, e.g., getc in ruby Is this possible? You

C reading (from stdin) stops at 0x1a character

狂风中的少年 提交于 2019-11-26 23:26:08
问题 currently I'm implementing the Burrows-Wheeler transform (and inverse transform) for raw data (like jpg etc.). When testing on normal data like textfiles no problems occur. But when it comes to reading jpg files e.g. it stops reading at character 0x1a aka substitute character. I've been searching through the internet for solutions which doesn't take OS dependend code but without results... I was thinking to read in stdin in binary mode but that isn't quite easy I guess. Is there any simple

Send string to stdin

浪子不回头ぞ 提交于 2019-11-26 23:23:28
Is there a way to effectively do this in bash: /my/bash/script < echo 'This string will be sent to stdin.' I'm aware that I could pipe the output from the echo such as this: echo 'This string will be piped to stdin.' | /my/bash/script You can use one-line heredoc cat <<< "This is coming from the stdin" the above is the same as cat <<EOF This is coming from the stdin EOF or you can redirect output from a command, like diff <(ls /bin) <(ls /usr/bin) or you can read as while read line do echo =$line= done < some_file or simply echo something | read param You were close /my/bash/script <<< 'This

fflush(stdin) function does not work

半世苍凉 提交于 2019-11-26 23:16:44
I can't seem to figure out what's wrong with this code: #include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #define MAX 100 #define TRUE 1 #define FALSE 0 char sect_cat; char customer_name[MAX]; char customer_number[MAX]; /* error handling is easier */ int prev_unit = 0; int current_unit = 0; int consumed = 0; int set = FALSE; float init_bill; float tax; float total_bill; void get_userinfo() { printf("Enter sector category: "); scanf("%c", &sect_cat); printf("Enter customer name: "); fflush(stdin); scanf("%sn", &customer_name); set = FALSE; while (set == FALSE) {