stdin

What does sys.stdin read?

你。 提交于 2019-12-17 18:59:17
问题 I get how to open files, and then use Python's pre built in functions with them. But how does sys.stdin work? for something in sys.stdin: some stuff here lines = sys.stdin.readlines() What's the difference between the above two different uses on sys.stdin? Where is it reading the information from? Is it via keyboard, or do we still have to provide a file? 回答1: So you have used Python's "pre built in functions", presumably like this: file_object = open('filename') for something in file_object:

Read input.txt file and also output.bmp file from terminal (C-programming)

老子叫甜甜 提交于 2019-12-17 17:16:02
问题 I have to do an assignment where I have to write a C-Programm, where it gets the input-file-name from the console as command line parameter. It should move the data from the input.txt file (the input file has the information for the bmp file - color etc.) to the generated output.png file. The 20 20 parameters stand for width and height for the output.png image. So the console-request for example (tested on Linux) will look like this: ./main input.txt output.bmp 20 20 I know that this code

Receiving data via stdin and storing as a variable/array

扶醉桌前 提交于 2019-12-17 17:15:10
问题 I am trying to receive data via stdin which I have working: const fs = require('fs'); const readStream = process.stdin; readStream.pause(); readStream.setEncoding('utf8'); readStream.on('data', (data) => { console.log(data); }); readStream.resume(); Now what I need to do is store it as a variable so I can do some calculations before I return it via stdout. Every time I try to so anything with it, like push certain data to an array it repeats the data until the stdin has finished, and I cant

Use of undefined constant STDIN - assumed 'STDIN' in C:\wamp\www\study\sayHello.php on line 5

拥有回忆 提交于 2019-12-17 16:29:19
问题 I want to Learn php & mySQL and I purchased a book (php&mySql: the missing manuals 2edition) I installed Wampserver2.4 on win8 64bit machine. Server Configuration Apache Version : 2.4.4 PHP Version : 5.4.12 in first lesson i got this error :( Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\wamp\www\study\sayHello.php on line 5 this is the php code on file "sayHello.php" <?php echo "Hello there. So I hear you're learning to be a PHP programmer!\n"; echo "Why don't you type in

Reading piped input with C++

耗尽温柔 提交于 2019-12-17 16:29:03
问题 I am using the following code: #include <iostream> using namespace std; int main(int argc, char **argv) { string lineInput = " "; while(lineInput.length()>0) { cin >> lineInput; cout << lineInput; } return 0; } With the following command: echo "Hello" | test.exe Thes result is an infinate loop printing "Hello". How can I make it read and print a single "Hello"? 回答1: string lineInput; while (cin >> lineInput) { cout << lineInput; } If you really want full lines, use: string lineInput; while

Can I use RSpec to mock stdin/stdout to test console reads & writes?

怎甘沉沦 提交于 2019-12-17 16:26:17
问题 My Ruby program reads lines from stdin and uses puts to print to stdout (the terminal). Can I use RSpec to test the reads and writes? Can I inject a string to my program like it was written in stdin and at the same time check the output? line = STDIN.read.chomp.split Also, I have the reads and writes in a loop, until line[0] is "quit". Can I test while the loop is running or should I call subject.read_in and subject.write_out ? 回答1: You can use mocks and have the method called more than once

Reading from stdin

给你一囗甜甜゛ 提交于 2019-12-17 15:47:13
问题 What are the possible ways for reading user input using read() system call in Unix. How can we read from stdin byte by byte using read() ? 回答1: You can do something like this to read 10 bytes: char buffer[10]; read(STDIN_FILENO, buffer, 10); remember read() doesn't add '\0' to terminate to make it string (just gives raw buffer). To read 1 byte at a time: char ch; while(read(STDIN_FILENO, &ch, 1) > 0) { //do stuff } and don't forget to #include <unistd.h> , STDIN_FILENO defined as macro in

What's the fastest way to read from System.in in Java?

感情迁移 提交于 2019-12-17 15:10:54
问题 I am reading bunch of integers separated by space or newlines from the standard in using Scanner(System.in) . Is there any faster way of doing this in Java? 回答1: Is there any faster way of doing this in Java? Yes. Scanner is fairly slow (at least according to my experience). If you don't need to validate the input, I suggest you just wrap the stream in a BufferedInputStream and use something like String.split / Integer.parseInt . A small comparison: Reading 17 megabytes (4233600 numbers)

Test if stdin has input for C++ (windows and/or linux)

大憨熊 提交于 2019-12-17 12:46:33
问题 I basically want to test if stdin has input (like if you echo and pipe it). I have found solutions that work, but they are ugly, and I like my solutions to be clean. On linux I use this: bool StdinOpen() { FILE* handle = popen("test -p /dev/stdin", "r"); return pclose(handle) == 0; } I know that I should add more error handling, but it's besides the point. On windows I use this: bool StdinOpen() { static HANDLE handle = GetStdHandle(STD_INPUT_HANDLE); DWORD bytes_left; PeekNamedPipe(handle,

How to read from stdin with fgets()?

白昼怎懂夜的黑 提交于 2019-12-17 07:21:18
问题 I've written the following code to read a line from a terminal window, the problem is the code gets stuck in an infinite loop. The line/sentence is of undefined length, therefore I plan to read it in parts into the buffer, then concatenate it to another string which can be extended via realloc accordingly. Please can somebody spot my mistake or suggest a better way of achieving this? #include <stdio.h> #include <string.h> #define BUFFERSIZE 10 int main (int argc, char *argv[]) { char buffer