stdin

Read input from redirected stdin with python

﹥>﹥吖頭↗ 提交于 2019-12-12 07:31:25
问题 I have this loop that reads lines from stdin until a newline is entered, however, this only works from typing in the input. How do I get the program to read lines from a redirected stdin via the command line? For instance: $ python graph.py < input.input Here is the loop I have to read lines from input: while 1: line = sys.stdin.readline() if line == '\n': break try: lines.append(line.strip()) except: pass 回答1: As others have mentioned, probably your condition line == '\n' never holds true.

C user input and Linked List

Deadly 提交于 2019-12-12 05:37:21
问题 I am trying to intake a string from user input and then compare that to my linked list that i have created previously in the code and find the location as to where the string should be inserted. And stop looping when user enters nothing and hits enter. I am able to intake the string and find the location to insert, but what I want to do is to loop until the user enters a blank input. This is causing my code to break somewhere and I am not quite sure why. I have inserted break points in my

How do I go about Flushing STDIN here?

。_饼干妹妹 提交于 2019-12-12 05:14:10
问题 I have a function (in C) that gets input from the user, (using scanf) stores it in an unsigned int, and returns the input to other functions that handle it: unsigned int input(void) { unsigned int uin; scanf("%u", &uin); return val; } I was wondering, being as I ought to flush stdin, I'd want to use a while loop using getc, vis-a-vis: while (getc != '\n') { ... } But, I'm not sure of how to: A) Do operations inside of getc, as in how I ought to handle checks and concatenate the value, to each

Issues with standard input in C

自古美人都是妖i 提交于 2019-12-12 04:56:56
问题 I'm making a simple program in C that reads an input. It then displays the number of characters used. What I tried first: #include <stdio.h> int main(int argc, char** argv) { int currentChar; int charCount = 0; while((currentChar = getchar()) != EOF) { charCount++; } printf("Display char count? [y/n]"); int response = getchar(); if(response == 'y' || response == 'Y') printf("Count: %d\n",charCount); } What happened: I would enter some lines and end it with ^D (I'm on Mac). The program would

How to get size of data available thru stdin with C++ [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-12 03:36:09
问题 This question already has answers here : WinAPI C++ client detect write on anonymous pipe before reading (3 answers) Closed 6 years ago . If I get a handle to stdin from a console app like so: HANDLE hStdIn = ::GetStdHandle(STD_INPUT_HANDLE); I can then read data from it: BYTE buff[32]; DWORD dwcbRead = 0; BOOL bReadRes = ::ReadFile(hStdIn, buff, SIZEOF(buff), &dwcbRead, NULL); My question is, how do I know how many bytes are available before I read them? PS. ReadFile seems to block if there

How to process raw data (in python)? [closed]

孤者浪人 提交于 2019-12-12 03:27:52
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . ***I'm not explaining this well so hopefully this edit makes more sense: basically I have to write code that will work for a ton of test cases, the input below is just an example. So I can't manually enter the input into my function Say I have the following input: 0 4 0,2,2,3 and I need to generate some sort of

Input only to bottom line of terminal

倖福魔咒の 提交于 2019-12-12 03:13:09
问题 I made a command-line chat client with Java, that sends the messages from stdin to the server and, at the same time, receives messages from the server to stdout at the same time. It's likely that somebody may send a message while you're writing one, so I want the bottom line of the terminal always reserved for input, so that output appears above it. I hope you understand my question and I apologize in advance if you don't. 回答1: The JCurses library will do exactly what you need. http:/

Read an integer from stdin (in C)

会有一股神秘感。 提交于 2019-12-12 03:04:33
问题 I want to write a C program that takes as input an integer, and outputs its square. Here is what I have tried. However, ./a.out < 3 outputs 32768, and ./a.out < 4 also outputs 32768. Where am I wrong? Thanks. #include <stdio.h> int main(){ int myInt; scanf("%d", &myInt); printf("%d\n",myInt*myInt); } 回答1: It looks like what you're trying to do is echo 4 | ./a.out the syntax for < is program < input_file whereas | is command_with_output | program 回答2: ./a.out < 4 This tries to read the file

How to pipe STDIN to a PHP-spawned proc?

末鹿安然 提交于 2019-12-12 02:46:36
问题 To my PHPeoples, This is sort of a weird PHPuzzle, but I'm wondering if it's PHPossible. The end goal is the equivalent functionality of mysql mydb < file.sql But with an API like this ./restore < file.sql Where restore is a PHP script like this #!/usr/bin/env php $cmd = "msyql mydb"; passthru($cmd, $status); However, I want to pass STDIN to the passthru command. The clear benefit here is that I can put restore somewhere in a pipeline and everything works peachy. Here's an example # would be

Python: piping Requests image argument to stdin

我的未来我决定 提交于 2019-12-12 02:14:08
问题 I am trying to send arguments to a subprocess' stdin. In my case, it is an image downloaded with Requsts. Here is my code: from subprocess import Popen, PIPE, STDOUT img = requests.get(url, stream=True) i = img.raw.read() proc = subprocess.Popen(['icat', '-'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) proc.communicate(i) #proc.stdin.write(i) # I tried this too Unfortunately, the subprocess does nothing, and I get no errors. What is wrong with my code, and is there a cross-platform solution? 回答1