stdin

Why aren't scanf(“%*[^\\n]\\n”); and scanf(“%*[^\\n]%*c”); clearing a hanging newline?

房东的猫 提交于 2019-12-05 16:19:49
After a call to scanf("%d", &variable); we are left with a newline hanging at the stdin , which should be cleared before a call to fgets , or we end up feeding it a newline and making it return prematurely. I've found answers suggesting using scanf("%*[^\n]%*c"); after the first call to scanf to discard the newline and others suggesting using scanf("%*[^\n]\n"); . Theoretically, both should work: The first would consume everything that isn't a newline (but not including the newline itself) and then consume exactly one character (the newline). The second would consume everything that isn't a

How can I test STDIN without blocking in Perl?

百般思念 提交于 2019-12-05 14:10:38
问题 I'm writing my first Perl app -- an AOL Instant Messenger bot that talks to an Arduino microcontroller, which in turn controls a servo that will push the power button on our sysadmin's server, which freezes randomly every 28 hours or so. I've gotten all the hard stuff done, I'm just trying to add one last bit of code to break the main loop and log out of AIM when the user types 'quit'. The problem is, if I try to read from STDIN in the main program loop, it blocks the process until input is

Should I set stdout and stdin to be unbuffered in C?

本小妞迷上赌 提交于 2019-12-05 13:01:52
Because of stdin and stdout buffering sometimes printf , scanf and getchar are not executed. I usually flush output buffer using fflush(stdout) but code can become very unreadable because of that. If I set stdin and stdout unbuffered using setbuf(stdin, NULL) and setbuf(stdout, NULL) will I make my program perform better or worse? Making stdin or stdout completely unbuffered can make your program perform worse if it handles large quantities of input / output from and to files. Most I/O requests will be broken down as system calls on a byte by byte basis. Note that buffering does not cause

Reading line by line from STDIN without blocking

孤人 提交于 2019-12-05 11:02:07
Basically, I'm looking to read lines from STDIN, but I don't want to block while waiting for new data. Almost like using a stream with a timeout. $stdin = fopen('php://stdin', 'r'); do { $line = fgets($stdin); // No input right now if (empty($line)) { // Do something before waiting for more input } } while (1); St. John Johnson Figured it out, use stream_set_blocking Docs to disable blocking. Sets $line to false when no input is available. 来源: https://stackoverflow.com/questions/4966365/reading-line-by-line-from-stdin-without-blocking

How do i run a program from another program and pass data to it via stdin in c or c++?

北慕城南 提交于 2019-12-05 10:34:30
Say i have an exe, lets say sum.exe. Now say the code for sum.exe is void main () { int a,b; scanf ("%d%d", &a, &b); printf ("%d", a+b); } I wanted to know how i could run this program from another c/c++ program and pass input via stdin like they do in online compiler sites like ideone where i type the code in and provide the stdin data in a textbox and that data is accepted by the program using scanf or cin. Also, i wanted to know if there was any way to read the output of this program from the original program that started it. How to do so is platform dependent. Under windows, Use CreatePipe

How to write unit tests for Inquirer.js?

孤街醉人 提交于 2019-12-05 09:41:21
I was wondering how to write unit tests for the npm package Inquirer.js , which is a tool to make CLI package more easily. I have read this post but I was't able to make it works. Here is my code that needs to be tested: const questions = [ { type: 'input', name: 'email', message: "What's your email ?", }, { type: 'password', name: 'password', message: 'Enter your password (it will not be saved neither communicate for other purpose than archiving)' } ]; inquirer.prompt(questions).then(answers => { const user = create_user(answers.email, answers.password); let guessing = guess_unix_login(user);

Python Twisted integration with Cmd module

拥有回忆 提交于 2019-12-05 08:20:31
I like Python's Twisted and Cmd . I want to use them together. I got some things working, but so far I haven't figured out how to make tab-completion work, because I don't see how to receive tab keypres events right away (without pressing Enter) in Twisted's LineReceiver. Here's my code so far: #!/usr/bin/env python from cmd import Cmd from twisted.internet import reactor from twisted.internet.stdio import StandardIO from twisted.protocols.basic import LineReceiver class CommandProcessor(Cmd): def do_EOF(self, line): return True class LineProcessor(LineReceiver): from os import linesep as

wkhtmltopdf relative paths in HTML with redirected in/out streams won't work

做~自己de王妃 提交于 2019-12-05 08:06:20
I am using wkhtmltopdf.exe (version 0.12.0 final) to generate pdf files from html files, I do this with .NET C# My problem is getting javascript, stylesheets and images to work by only specifying relative paths in the html. Right now I have it working if I use absolute paths. But it doesn't work with relative paths, which makes the whole html generation a bit to complicated. I have boiled what I do down to the following example: string CMDPATH = @"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"; string HTML = string.Format( "<div><img src=\"{0}\" /></div><div><img src=\"{1}\" /></div><div>{2

C: Read from stdin until Enter is pressed twice

时光怂恿深爱的人放手 提交于 2019-12-05 07:59:48
Consider a simple program. It must take sequences of 5 numbers from stdin and print their sums. It is not stated how many lines of input will be taken, but program must terminate if newline character is taken twice (or Enter is pressed twice). For example, Input: 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3/n /n Output: 5 10 15 #include <stdio.h> int main() { int n1, n2, n3, n4, n5; int sum; while (/*condition*/) { scanf ("%d %d %d %d %d\n", &n1, &n2, &n3, &n4, &n5); sum = n1 + n2 + n3 + n4 + n5; printf ("%d\n", sum); } return 0; } The only problem is I don't know what condition must be in a while-loop. A

Write to stdin of a running process in windows

纵然是瞬间 提交于 2019-12-05 07:10:45
I want to write data to an existing process's stdin from an external process in Windows. I found similar questions for Linux, but I want to know how I can do the same in Windows. How to write data to existing process's STDIN from external process? How do you stream data into the STDIN of a program from different local/remote processes in Python? https://serverfault.com/questions/443297/write-to-stdin-of-a-running-process-using-pipe I tried with this code but I got an error. I also tried running the program, sending stdin to that with this code, but again, it errored. In CMD: type my_input