stdin

read() from stdin doesn't ignore newline

喜夏-厌秋 提交于 2019-11-28 11:36:21
I am using the following conditional statement to read from standard input. if ((n = read(0,buf,sizeof(buf))) != 0) When inputting data from standard input, generally the user presses enter when done. But read() considers '\n' as input too in which case n = 1 and the conditional doesn't evaluate to false. Is there a way to make the conditional evaluate to false when the user presses enter (without entering anything) on standard input apart from checking the contents of buf. Is there any other function other than read() that I might use for this purpose?? For that matter, what can be a way for

Testing STDIN in Ruby

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 11:05:08
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 equivalent $stdout & $stdin: def ask_for_mark ouput.puts 'What shall I call you today?' answer = input.gets

What does sys.stdin read?

爷,独闯天下 提交于 2019-11-28 10:26:38
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? So you have used Python's "pre built in functions", presumably like this: file_object = open('filename') for something in file_object: some stuff here This reads the file by invoking an iterator on the file object which happens to return the

Fastest method to generate big random string with lower Latin letters

有些话、适合烂在心里 提交于 2019-11-28 09:57:45
I'm trying to solve this problem from Timus Online Judge. To solve this problem you need generate a sequence of 1 000 000 lowercase Latin letters and write it to stdin in 1 second. It is easy to solve this problem with C++ or Java. I have python solution here: import os from random import randint s = ''.join(chr(97 + randint(0, 25)) for i in range(1000000)) os.write(1, bytes(s, 'utf8')) It takes 1.7s: $ time python3.3 1219.py > /dev/null real 0m1.756s user 0m1.744s sys 0m0.008s And I got "Time limit exceeded" in result. So the question is "How to do it faster?" UPD1 : Using randint(97, 122)

aysncio cannot read stdin on Windows

£可爱£侵袭症+ 提交于 2019-11-28 09:12:09
I'm trying to read stdin asynchronously on Windows 7 64-bit and Python 3.4.3 I tried this inspired by an SO answer : import asyncio import sys def reader(): print('Received:', sys.stdin.readline()) loop = asyncio.get_event_loop() task = loop.add_reader(sys.stdin.fileno(), reader) loop.run_forever() loop.close() However, it raises an OSError: [WInError 100381] An operation was attempted on something that is not a socket . Could a file-like object like stdin be wrapped in a class to give it the API of a socket? I have asked this question separately , but if the solution is simple please answer

Recognizing arrow keys with stdin

独自空忆成欢 提交于 2019-11-28 08:32:22
is it possible to have a cross-platform way to handle backspace and arrows keys within a C or OCaml program? Actually an OCaml solution would be appreciated but many standard unix functions are wrapped directly to corresponding API calls so there's should be no problem in porting a C solution. What I'm going to achieve is to catch the arrow keys to override its behaviour inside the shell (by repropting last line or operations like these). I think that this thing falls before the actual program and it's not handled by code itself so I don't know if it's possible. The program is compiled either

Resume reading from iostream::cin after Ctrl+Z (EOF)? (“ignore” doesn't work)

纵然是瞬间 提交于 2019-11-28 08:16:40
问题 Why does the outer loop in the following program terminate when we provide ctrl+z for the inner loop only? #include<iostream> int main() { string s1,s2; while(cin >> s1) { cout<<"In loop1\n"; while(cin>>s2) cout<<"In loop 2\n"; cin.ignore(); } } 回答1: Hitting Ctrl+z (on Windows) closes the standard input stream. Once it's closed, it stays closed. It doesn't magically reopen once the inner loop is finished. There's just no reason why it would. 回答2: Ctrl-Z puts cin into an error state so cin

Why does supplying stdin to subprocess.Popen cause what is written to stdout to change?

不想你离开。 提交于 2019-11-28 07:49:30
问题 I'm using Python's subprocess.Popen to perform some FTP using the binary client of the host operating system. I can't use ftplib or any other library for various reasons. The behavior of the binary seems to change if I attach a stdin handler to the Popen instance. For example, using XP's ftp client, which accepts a text file of commands to issue: >>>from subprocess import Popen, PIPE >>>p = Popen(['ftp','-A','-s:commands.txt','example.com'], stdout=PIPE) >>>p.communicate()[0] 'Connected to

How can I reinitialize Perl's STDIN/STDOUT/STDERR?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 07:34:45
I have a Perl script which forks and daemonizes itself. It's run by cron, so in order to not leave a zombie around, I shut down STDIN,STDOUT, and STDERR: open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!"; open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!"; if (!fork()) { do_some_fork_stuff(); } The question I have is: I'd like to restore at least STDOUT after this point (it would be nice to restore the other 2). But what magic symbols do I need to use to re-open STDOUT as what STDOUT used to be? I know that

Read a character from standard input in Go (without pressing Enter)

左心房为你撑大大i 提交于 2019-11-28 07:33:08
I want to my app shows: press any key to exit ... And when I pressed any key, it exits. How can I achieve this? Note: I have googled but all of what I've found needed to press enter at the end. I want something like Console.ReadKey() in C#. I am running MS Windows. termbox-go is a light-weight Go-native package which offers some rudimentary terminal control. Including the ability to get input in raw mode (read one character at a time without the default line-buffered behaviour). It also has fairly ok compatibility across different systems. And keyboard extends termbox-go to give some