stdin

Is it possible to set timeout for std::cin?

送分小仙女□ 提交于 2019-11-27 09:16:02
Is it possible to set timeout for std::cin? For example, std::cin doesn't receive any data during 10 seconds - it throws an exception or returns an error. Edited: And what about timer from Boost library ? As far as I know, it is portable library. Is it possible to ask timer of Boost library to throw exceptions after predefined period of time? I guess it can solve this problem. It isn't possible to set a time out for std::cin in a portable way. Even when resorting to non-portable techniques, it isn't entirely trivial to do so: you will need to replace std::cin 's stream buffer. On a UNIX system

Python using STDIN in child Process

落花浮王杯 提交于 2019-11-27 07:52:06
问题 So I have a program, in the "main" process I fire off a new Process object which (what I want) is to read lines from STDIN and append them to a Queue object. Essentially the basic system setup is that there is a "command getting" process which the user will enter commands/queries, and I need to get those queries to other subsystems running in separate processes. My thinking is to share these via a multiprocessing Queue which the other systems can read from. What I have (focusing on just the

How to write data to existing process's STDIN from external process?

久未见 提交于 2019-11-27 07:50:31
I'm seeking for ways to write data to the existing process's STDIN from external processes, and found similar question How do you stream data into the STDIN of a program from different local/remote processes in Python? in stackoverlow. In that thread, @Michael says that we can get file descriptors of existing process in path like below, and permitted to write data into them on Linux. /proc/$PID/fd/ So, I've created a simple script listed below to test writing data to the script's STDIN (and TTY ) from external process. #!/usr/bin/env python import os, sys def get_ttyname(): for f in sys.stdin,

Scanf/Printf double variable C

倖福魔咒の 提交于 2019-11-27 07:48:10
Let's say I have this following bit of code in C: double var; scanf("%lf", &var); printf("%lf", var); printf("%f", var); It reads from stdin variable 'var' and then prints twice in stdout 'var'. I understand that's how you read a double variable from stdin, but my questions are: Why can you print a double with %lf? Why can you print a double with %f? Which one is better and correct to use? For variable argument functions like printf and scanf , the arguments are promoted, for example, any smaller integer types are promoted to int , float is promoted to double . scanf takes parameters of

Reading from a file with sys.stdin in Pycharm

穿精又带淫゛_ 提交于 2019-11-27 06:50:39
问题 I am trying to test a simple code that reads a file line-by-line with Pycharm. for line in sys.stdin: name, _ = line.strip().split("\t") print name I have the file I want to input in the same directory: lib.txt How can I debug my code in Pycharm with the input file? 回答1: You need to create a custom run configuration and then add your file as an argument in the "Script Parameters" box. See Pycharm's online help for a step-by-step guide. However, even if you do that (as you have discovered),

Testing STDIN in Ruby

浪子不回头ぞ 提交于 2019-11-27 05:54:31
问题 I'm currently trying to test a basic method that receives some input from the user (gets) and outputs it (puts). After a bit of research I found a good way to test the standard output stream which is the below: def capture_standard_output(&block) original_stream = $stdout $stdout = mock = StringIO.new yield mock.string.chomp ensure $stdout = original_stream end The method I'm testing is the one below, output and input refer to ivars which I initialize in the beginning and point to the

Using fseek with a file pointer that points to stdin

倾然丶 夕夏残阳落幕 提交于 2019-11-27 05:25:51
Depending on command-line arguments, I'm setting a file pointer to point either towards a specified file or stdin (for the purpose of piping). I then pass this pointer around to a number of different functions to read from the file. Here is the function for getting the file pointer: FILE *getFile(int argc, char *argv[]) { FILE *myFile = NULL; if (argc == 2) { myFile = fopen(argv[1], "r"); if (myFile == NULL) fprintf(stderr, "File \"%s\" not found\n", argv[1]); } else myFile = stdin; return myFile; } When it's pointing to stdin, fseek does not seem to work. By that, I mean I use it and then use

C, flushing stdin

和自甴很熟 提交于 2019-11-27 04:43:35
问题 i have a problem with the use of fgets. The loop is supposed to read a line of max. 19 characters, analyze this char array and then wait for next input. The problem is that if the line entered exeeds 19 characters, fgets will fill str with the remaining characters untill Ctrl-D or newline is entered, thus initiating a new loop without new input. The input (stdin) should in some way be flushed after 19 characters are read, so the loop can start with a clean slate. Anyone have a solution to

End of File in stdin

跟風遠走 提交于 2019-11-27 04:41:28
问题 A question about this has been asked here End of File (EOF) in C but it still doesn't completely solve my problem. EOF makes sense to me in any datastream which is not stdin , for example if I have some data.txt file, fgetc() will read all the chars and come to the end of file and return -1 . What I don't understand is the concept of EOF in stdin . If I use getchar() , it will wait for me to enter something, so if there is NOTHING written, End of File, ( EOF ) is not returned automatically?

node.js: readSync from stdin?

℡╲_俬逩灬. 提交于 2019-11-27 04:18:27
问题 Is it possible to synchronously read from stdin in node.js? Because I'm writing a brainfuck to JavaScript compiler in JavaScript (just for fun). Brainfuck supports a read operation which needs to be implemented synchronously. I tried this: const fs = require('fs'); var c = fs.readSync(0,1,null,'utf-8'); console.log('character: '+c+' ('+c.charCodeAt(0)+')'); But this only produces this output: fs:189 var r = binding.read(fd, buffer, offset, length, position); ^ Error: EAGAIN, Resource