stdin

How to know when stdin is empty if it contains EOF?

柔情痞子 提交于 2019-12-11 03:13:25
问题 In an attempt to create a simple cat clone in python, sys.stdout.write(sys.stdin.read()) I noticed that this fails horribly for binary files (i.e. python cat.py < binaryfile > supposed_copy ) containing the CTRL + Z EOF / substitude character 0x1a, since that seems to cause read() to consider its work done. I cannot simply loop over the code forever to circumvent this, since obviously at some point stdin.read() will wait until new input is provided, which once the true end of input is reached

How can I write to stdIn (JAVA) [duplicate]

北城以北 提交于 2019-12-11 03:02:17
问题 This question already has answers here : JUnit testing with simulated user input (7 answers) Closed 4 years ago . I want to do some tests on my P2P system by using some input like: "join 8". 8 is the Node number. For my system, the command "join 8" is read from stdin, but I don't want to type it hundred times for hundred tests, so I write a test function to randomly generate node numbers and then call the "join" command by itself. So I want JAVA to write commands instead of my own input to

Why is the first string ignored when reading with fgets from stdin? [duplicate]

拜拜、爱过 提交于 2019-12-11 02:07:16
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Dev-C++ Input skipped I am trying to read an array of character strings from stdin using fgets, but the first string I want to read is always ignored. What is causing this issue? #include <stdio.h> int main() { int i; struct material { char name[30]; float price, kg; }; unsigned m,nr; printf("Lorry capacity="); scanf("%u", &m); printf("Number of materials="); putchar('\n'); scanf("%u", &nr); struct material list

Command line arguments in bash script

旧城冷巷雨未停 提交于 2019-12-11 01:31:12
问题 I am trying to read command line arguments in bash but I have problems to read on the 10th column Here is my sample script: #------------------------------------------------------- #!/bin/bash an=$2 mn=$4 dy=$6 der=$8 new=$10 sec=(${12} ${13}) echo $an $mn $dy $der $new $sec #-------------------------------------------------------- I have run the above script "test.sh" as ./test.sh -yr cat -mn Jan -dy tuesday -der tt -new car -sec 001 001 The output is: cat Jan tuesday tt -yr0 001 But for

How to do nonblocking input from stdin in C [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-11 00:32:43
问题 This question already has answers here : How to read terminal's input buffer immediately after keypress (2 answers) Closed 3 years ago . I have follow situation I have an program make an set of operations on a file continuously and I want, when a specific key is pressed, to stop and do another set of operations. For this I tryed use scanf of an character with fcntl(0, F_SETFL, O_NONBLOCK); and while(feof(stdin)) but it doesn't work as expected. I have searched and in some places someone says

Why can I not read from stdin in this forked process?

对着背影说爱祢 提交于 2019-12-10 23:36:40
问题 The following code prints nothing, but it should print out "a" repeatedly. The forked process blocks on the os.read(0, 1). The parent process is indeed writing to the stdin_master, but stdin_slave receives nothing. Any ideas? import os import pty import resource import select import signal import time stdin_master, stdin_slave = pty.openpty() stdout_master, stdout_slave = pty.openpty() stderr_master, stderr_slave = pty.openpty() pid = os.fork() # child process if pid == 0: os.setsid() os

Input using gets not working, but working with scanf

家住魔仙堡 提交于 2019-12-10 23:21:50
问题 #include<stdio.h> void main(){ char str[100]; char letter; letter=getchar(); printf("%c",letter); gets(str); //Rest of code } On execution, the code skips the gets(str) line. But when I replace gets by scanf, it works. Any specific reason why that doesnt work? I am using gcc 4.7.2. 回答1: The first call to getchar() leaves a newline character in the input buffer. The next call gets() considers that newline as end of input and hence doesn't wait for you to input. Consume the newline character

Runtime.exec never returns when reading system.in

眉间皱痕 提交于 2019-12-10 22:55:54
问题 Here is my sample code, I want to handle the command from standard input while running a new sub process. However, the exec method never returns if I read the system.in. The command in the exec() is very simple and has nothing to do with the stdin. I'm wondering about is there any way to solve this? How can I start a new sub process while start another thread reading stdin? public static void main(String[] args){ new Thread(new Runnable(){ public void run(){ BufferedReader reader = new

PHP Get user input without user having to press return key

亡梦爱人 提交于 2019-12-10 20:55:49
问题 Hi I am using PHP in CLI mode (Command Line Interface) I would like to get the key the user types and immediately have it submitted to the program without the user having to press the return key(Enter Key) So for example's sake I'ld like it to print the letter the user types immediately. So if the user types an "a" it immediately shows an "a" in the command prompt. How would I do this? do { $selection = fgetc(STDIN); fwrite(STOUT, "$selection"); } while ( trim($selection) == '' ); 回答1: There

Perl: Testing an input reader?

末鹿安然 提交于 2019-12-10 20:44:35
问题 Is there a way to automatically test using the standard Test etc. modules whether a Perl program is reading input from e.g. STDIN properly? E.g. testing a program that reads two integers from STDIN and prints their sum. 回答1: It's not 100% clear what you mean, I'll asnswer assuming you want to write a test script that tests your main program, which as part of the test needs to have test input data passed via STDIN. You can easily do that if your program outputs what it reads. You don't need a