stdin

How to read from std::cin until the end of the stream?

亡梦爱人 提交于 2019-11-29 15:47:48
My problem is, that I want to read the input from std::cin but don't know how long the input is. Also I have to char and can't use std::string . There are two ways I have to handle: a) The user inputs text and when he hits [ENTER] the program stops reading. b) The user redirects std::cin to a file (like .\a.oput < file ) which can hold multiple lines. Edit: Just noticed that std::cin.eof() is always false also in the case of reading form a file. For a) I could read until \n occures. For b) Edit: No (I could read until std::cin.eof() occures.) But when I don't know whether I'm getting an a) or

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

妖精的绣舞 提交于 2019-11-29 14:42:07
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(); } } 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. Ctrl-Z puts cin into an error state so cin.ignore does nowt. try cin.Clear() instead. 来源: https://stackoverflow.com/questions/10147996/resume-reading-from

Is it possible to pipe a script to WScript?

一世执手 提交于 2019-11-29 14:31:21
I would like to execute a simple VBScript/JScript generated by my application using the Windows Script Host (wscript.exe or cscript.exe) without generating a temporary script file. I did not find a command line option to read the script form the standard input stream, but i did find several places in the WWW that document a WScript error message "Can't read script from stdin." MSDN Windows Script Host Reference - Error Messages So is there a hidden command line option to read the script from standard in? Currently my program generates a small script and writes it to a temporary file. Then it

Reading in integer from stdin in Python

时光毁灭记忆、已成空白 提交于 2019-11-29 14:12:41
I have the following piece of code where I take in an integer n from stdin, convert it to binary, reverse the binary string, then convert back to integer and output it. import sys def reversebinary(): n = str(raw_input()) bin_n = bin(n)[2:] revbin = "".join(list(reversed(bin_n))) return int(str(revbin),2) reversebinary() However, I'm getting this error: Traceback (most recent call last): File "reversebinary.py", line 18, in <module> reversebinary() File "reversebinary.py", line 14, in reversebinary bin_n = bin(n)[2:] TypeError: 'str' object cannot be interpreted as an index I'm unsure what the

In Linux, why is there a global /dev/stdin file for all processes?

爱⌒轻易说出口 提交于 2019-11-29 14:03:06
Shouldn't the standard input for different process unique? If so, shouldn't the path of the stdin file be like /dev/pid/stdin instead of a global /dev/stdin ? Does anyone have ideas about this? /dev/stdin is unique because it is a symbolic link to /proc/self/fd/0 /proc/self is a symbolic link only seen by your running process to its process-id The /proc filesystem is a virtual (not real ) filesystem which has the ability to show a different view to each process. Further reading: Linux Filesystem Hierarchy: 1.14. /proc Red Hat Enterprise Linux 3: Reference Guide: Chapter 5. The proc File System

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

我们两清 提交于 2019-11-29 14:02:49
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 example.com. 220 ProFTPD 1.3.1 Server (Debian) ... 331 Anonymous login ok, send your complete email

Can I prompt for user input after reading piped input on STDIN in Perl?

梦想与她 提交于 2019-11-29 13:11:11
问题 I'm trying to write a perl script that reads piped data, then prompts the user for input based on that data. The following script, prompt_for_action , is what I am trying to do: #!/usr/bin/perl my @hosts = (); while (<>) { my $host = $_; $host =~ s/\n//; # strip newlines push(@hosts, $host); } for my $host (@hosts) { print "Do you want to do x with $host ? y/n: "; chomp(my $answer = <>); print "You said `$answer`.\n"; } but when I run it there is no waiting for user input, it just blows

Read unknown number of lines from stdin, C

和自甴很熟 提交于 2019-11-29 12:39:01
i have a problem with reading stdin of unknown size. In fact its a table in .txt file, which i get to stdin by calling parameter '<'table.txt. My code should look like this: #include <stdio.h> #include <string.h> int main(int argc,char *argv[]) { char words[10][1024]; int i=0; while(feof(stdin)==0) { fgets(words[i],100,stdin); printf("%s", words[i]); i++; } return 0; } but there is the problem i dont know the nuber of lines, which in this case is 10(we know the number of characters in line - 1024). It would be great if someone know the solution. Thanks in advance. You have hit on one of the

Using sys.stdin.readline() to read multiple lines from cmd in Python

落花浮王杯 提交于 2019-11-29 12:23:30
I'd like to type in my input from command lines after running if __name__ == "__main__": data = list(map(int, sys.stdin.readline().split())) print(data) n, capacity = data[0:2] values = data[2:(2 * n + 2):2] weights = data[3:(2 * n + 2):2] A sample input could be: 2 40 20 2 30 3 My questions are: 1) How to create the list data using my input? 2) How can I let Python know I have finished the input and it should execute the rest of the code? The solution to this problem depends on the OS you're using. Basically, if you want multiline input, you'll have to use sys.stdin.read() instead of sys

Best way to read binary file c++ though input redirection

时光总嘲笑我的痴心妄想 提交于 2019-11-29 12:06:47
I am trying to read a large binary file thought input redirection ( stdin ) at runtime, and stdin is mandatory. ./a.out < input.bin So far I have used fgets. But fgets skips blanks and newline. I want to include both. My currentBuffersize could dynamically vary. FILE * inputFileStream = stdin; int currentPos = INIT_BUFFER_SIZE; int currentBufferSize = 24; // opt unsigned short int count = 0; // As Max number of packets 30,000/65,536 while (!feof(inputFileStream)) { char buf[INIT_BUFFER_SIZE]; // size of byte fgets(buf, sizeof(buf), inputFileStream); cout<<buf; cout<<endl; } Thanks in advance.