stdin

How to read n integers from standard input in C++?

对着背影说爱祢 提交于 2019-11-30 00:03:09
I need to read something like: 5 60 35 42 2 38 6 5 8 300 1500 900 And then save the first line in an array. After calling other functions do the same with the next line, and so on. I try with gets() and then use sscanf() to scan the integers from the string, but I don't know how to read n numbers from a string. I've seen input files like this for competitions before. If speed is more of an issue than error detection, you could use a custom routine. Here's one similar to that I use: void readintline(unsigned int* array, int* size) { char buffer[101]; size=0; char* in=buffer; unsigned int* out

Pipe input to Python program and later get input from user

爱⌒轻易说出口 提交于 2019-11-29 23:14:30
Let's say I want to pipe input to a Python program, and then later get input from the user, on the command line. echo http://example.com/image.jpg | python solve_captcha.py and the contents of solve_captcha.py are: import sys image_url = sys.stdin.readline() # Download and open the captcha... captcha = raw_input("Solve this captcha:") # do some processing... The above will trigger a EOFError: EOF when reading a line error. I also tried adding a sys.stdin.close() line, which prompted a ValueError: I/O operation on closed file . Can you pipe information to stdin and then later get input from the

Python equivalent of Perl's while (<>) {…}?

一世执手 提交于 2019-11-29 23:04:09
I write a lot of little scripts that process files on a line-by-line basis. In Perl, I use while (<>) { do stuff; } This is handy because it doesn't care where the input comes from (a file or stdin). In Python I use this if len(sys.argv) == 2: # there's a command line argument sys.stdin = file(sys.argv[1]) for line in sys.stdin.readlines(): do stuff which doesn't seem very elegant. Is there a Python idiom that easily handles file/stdin input? The fileinput module in the standard library is just what you want: import fileinput for line in fileinput.input(): ... import fileinput for line in

Optional stdin in Python with argparse

試著忘記壹切 提交于 2019-11-29 21:18:23
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 I'm not using -i or redirecting input with one of $ someprog | my_python_prog $ my_python_prog < inputfile I don't want my Python program to wait for input. I want it to just move along and use default values. The standard library documentation for argparse suggests this solution to allow optional input/output files: >>> parser = argparse

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

穿精又带淫゛_ 提交于 2019-11-29 20:23:57
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 read command, because it stops reading at newline. I can read stdin with cat , like this: my_var=`cat /dev/stdin` , but then I don't know how to print it. So that the newline, tab, and other delimiters are still there. My sample script looks like this: #!/usr/local/bin/bash A=`cat /dev/stdin` if [ ${#A} -eq 0 ]; then exit 0 else cat ${A} | /usr

Opening a TStream on stdin/stdout in a Delphi console app

孤街醉人 提交于 2019-11-29 20:22:08
I'm trying to write a Delphi console application that creates a TStream for its standard input, and another TStream for its standard output. (It will be launched by a host app with its input and output redirected to pipes, and will be passing binary data to/from that host app, so TStream will be much better-suited to the task than ReadLn/WriteLn.) How do I go about opening a TStream on standard input or standard output? Allen Bauer Off the top of my head: InputStream := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE)); OutputStream := THandleStream.Create(GetStdHandle(STD_OUTPUT_HANDLE));

Not able to send commands to cmd.exe process

為{幸葍}努か 提交于 2019-11-29 18:49:26
I am trying to send commands to an open cmd.exe process using StandardInput.WriteLine(str) , however none of the commands seem to be sent. First I open a process, with a global variable p ( Process p ). p = new Process() { StartInfo = { CreateNoWindow = true, UseShellExecute = false, RedirectStandardError = true, RedirectStandardInput = true, RedirectStandardOutput = true, FileName = @"cmd.exe", Arguments = "/C" //blank arguments } }; p.Start(); p.WaitForExit(); After, I try to send a command using a simple method, that logs the result in a text box. private void runcmd(string command) { p

How to check if stdin is still opened without blocking?

倖福魔咒の 提交于 2019-11-29 18:34:31
问题 I need my program written in pure C to stop execution when stdin is closed. There is indefinite work done in program main cycle, and there is no way I can use blocking checks (like getc() ) there (no data is supposed to arrive on stdin - it just stays opened for unknown time). I intend to use described functionality in realization of network daemon hosted in inetd, xinetd or their analogs - it should emit data on stdout while connection stays opened and correctly finish work when it closes.

Call an exe from Java with passing parameters with writing to stdout and reading from stdin

给你一囗甜甜゛ 提交于 2019-11-29 18:09:48
I have read this question: Java Programming: call an exe from Java and passing parameters And this answer is good enough https://stackoverflow.com/a/5604756/2674303 But I additionally want to pass parameters to stdin of external process and read from stdout of this process. How can I do this? My efforts : main method: public class ProcessBuilderTest { public static void main(String[] args) throws IOException, InterruptedException { ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\Java\\jdk1.8.0_111\\bin\\java", "-cp", //class path key "C:\\Users\\redwhite\\IdeaProjects\\HelloMyWorld\

How to use Scanner to read silently from STDIN in Java?

戏子无情 提交于 2019-11-29 17:44:35
问题 I want to make a Java program that reads a Password from STDIN silently. I mean, without outputting any pressed chars to the terminal and keeping it hidden from commandline history and the operating system processlist ps . 回答1: The class java.io.Console may be useful: System.console().readPassword(); This reads a sequence of chars from the console, without echoing anything. Note that it only works when you launch your java application with a real console. Otherwise, System.console() returns