stdin

How can I select() (ie, simultaneously read from) standard input *and* a file in bash?

大城市里の小女人 提交于 2019-12-13 00:05:10
问题 I have a program that accepts input on one FIFO and emits output to another FIFO. I want to write a small script to control this program. The script needs to listen both to standard input (so I can input commands to adjust things in real time) and the program's output FIFO (so it can respond to events happening there as well). Essentially my control program needs to select between standard input and a file (my FIFO). I like learning how to figure out how to develop simple and elegant bash

why use EOF to check if stdin buffer is cleared

感情迁移 提交于 2019-12-12 22:18:17
问题 Say I have the following... int main () { char name [5] = ""; char c; printf("Enter a name: "); fgets(name,5,stdin); while ((c = getchar()) != '\n'); printf("Exiting...); getchar(); return 0; } The while loop will clean the stdin buffer but I have seen the loop done like this as well... while ((c = getchar()) != '\n' && c != EOF); I am wondering if there is any difference between the 2??? Does testing for EOF make any difference? 回答1: I am wondering if there is any difference between the 2???

Redirecting stdin through a FIFO

不打扰是莪最后的温柔 提交于 2019-12-12 20:01:47
问题 I'm running a server app (written in Java) under GNU/Linux which takes input (from stdin, I guess) and interprets it to run some commands. I dont want to run the app inside a terminal window (I'd like to run a daemon), but I'd still like to be able to input commands whenever I want to. I thought I might be able to do that using fifos, so I created it using mknod. The problem is cat fifofile > java... and cat fifofile | java ... fail with a "file not found" error for some reason. Using only

Pipe custom stdin to system call in C++

耗尽温柔 提交于 2019-12-12 19:19:02
问题 I'm trying to call a shell script from C++ with custom input. What I could do is: void dostuff(string s) { system("echo " + s + " | myscript.sh"); ... } Of course, escaping s is quite difficult. Is there a way that I can use s as stdin for myscript.sh? Ie, something like this: void dostuff(string s) { FILE *out = stringToFile(s); system("myscript.sh", out); } 回答1: A simple test to reassign stdin and restore it after the system call: #include <cstdlib> // system #include <cstdio> // perror

PHP: Read from socket or STDIN

安稳与你 提交于 2019-12-12 12:35:37
问题 I am learning socket programming in PHP and so I am trying a simple echo-chat server. I wrote a server and it works. I can connect two netcats to it and when I write in the one netcat, I recive it at the other. Now, I want to implement what NC does in PHP I want to use stream_select to see if I have data on STDIN or on the socket to either send the message from STDIN to the server or reading the incoming message from the server. Unfortunately the example at the php manual doesn't give me any

Reading from standard input using Flask-Script / Python

痴心易碎 提交于 2019-12-12 12:35:32
问题 Right now I have flask-script command that takes a path as an argument, then reads from the path: @manager.option('-f', '--file', dest='file_path') def my_command(file_path): open(file_path) ... I'd want it to be able to read from standard in as well. (I frequently need to pass it text on the clipboard, and it's annoying to have to create a file each time.) How can I accomplish this? I've tried using fileinput.input() , via this https://stackoverflow.com/a/1454400/1164573, invoked with the

Reading from stdin using read(..) and figuring out the size of the buffer

久未见 提交于 2019-12-12 11:45:08
问题 I was wondering if anyone could tell me if there is a way to dynamically allocate a buffer when reading an input from stdin using read(...) For example: n = read(0, buffer, sizeof ?); How do I ensure that the number of bytes read from stdin (here 0) is the same as in buffer ? 回答1: You can't. You do a read into a fixed-size buffer, e.g.: char buf[BUF_SIZE]; int num_read = read(0, buf, BUF_SIZE); and then figure out if there's any more data available (usually by checking whether num_read is

Peek stdin using pthreads

不羁的心 提交于 2019-12-12 09:56:25
问题 I'm trying to peek stdin to see if anything is there using pthreads. I (think I) need to do this because if there is nothing in std in, stream access functions will block for input. I feel the way to do this is to fire off a pthread that checks stdin, and sleep(1) and see if the thread found anything. Here's what I have so far. If nothing is piped into the program then it will sleep as expected, but if something is in stdin the thread never gets fired. #include <iostream> #include <pthread.h>

Perl with Sublime Text 2: <STDIN> not working

眉间皱痕 提交于 2019-12-12 09:10:49
问题 I'm new to Perl and am trying to write a simple program that prompts the user to enter a number. I then want that number to be printed. I think I have the correct code, but when I run the program I'm not able to enter anything. I'm using Sublime Text 2. Am I missing a plugin or something? How do I get this to work? I've only done simple if/else statements prior to this and everything worked. I just can't seem to prompt the user for input. Here is the code: print ("Please enter a number: \n");

using select to read from socket and stdin

孤街浪徒 提交于 2019-12-12 08:54:45
问题 I'm writing a ncurses based chat program. At first, I wrote just networking stuff (without ncurses) and everything worked fine, but after adding graphics I can't get the client app to work properly. The main problem is reading from stdin and socket at the same time. In ncurses-less version I've used pthread and it worked like charm. Alas, it seems that pthread and ncurses don't go together very well, so I had to find another solution. I thought that select() would do, but it still only reads