我使用以下代码:
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
string lineInput = " ";
while(lineInput.length()>0) {
cin >> lineInput;
cout << lineInput;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用以下命令:
echo "Hello" | test.exe
结果是无限循环打印"Hello".如何让它读取并打印单个"Hello"?
我想做的事情如下:
for line in sys.stdin:
do_something()
if is **END OF StdIn**:
do_something_special()
Run Code Online (Sandbox Code Playgroud)
经过几次尝试,现在我这样做:
while True:
try:
line = sys.stdin.next()
print line,
except StopIteration:
print 'EOF!'
break
Run Code Online (Sandbox Code Playgroud)
或者用这个:
while True:
line = sys.stdin.readline()
if not line:
print 'EOF!'
break
print line,
Run Code Online (Sandbox Code Playgroud)
我认为以上两种方式非常相似.我想知道有更优雅(pythonic)的方式吗?
我首先尝试StopIteration从for循环内部或外部捕获,但我很快意识到,由于StopIteration异常是构建到 for循环本身,所以下面的代码片段都不起作用.
try:
for line in sys.stdin:
print line,
except StopIteration:
print 'EOF'
Run Code Online (Sandbox Code Playgroud)
要么
for line in sys.stdin:
try:
print line,
except StopIteration:
print 'EOF'
Run Code Online (Sandbox Code Playgroud) 我想使用压缩stdin的内容zip,例如:
echo 'foo bar' | zip > file.zip
Run Code Online (Sandbox Code Playgroud)
这工作正常,但解压缩时,未压缩的文件名是 -
我想知道如何为stdin指定文件名?
我已经阅读了大约5-10个不同的建议如何清除标准输入,但它们都不适合我的需要.事情就是fflush(stdin)在我的计算机上完美运行,但不幸的是它似乎无处不在,所以我需要具有相同功能的东西.我想尽其他办法清除标准输入时,它不是空的,但需要用户输入时标准输入是空的,这意味着它需要在一个时刻,我不想得到任何输入(+后将其丢弃也无妨).
问题是:stdin在我需要用户输入之前,我可以以某种方式确定IS是空的吗?(如果不是,那么,然后才以某种方式清除它?)类似于:
if (stdin is NOT empty)
while (getchar() != '\n')
continue;
Run Code Online (Sandbox Code Playgroud)
编辑:问题是我stdin逐个加载字符,在某些时候,上一次迭代的输入的一部分可能会或可能不会被丢弃.无论哪种方式,stdin在我要求用户处理另一个输入之前我需要清楚.清除缓冲区本身是没有这样一个大问题,问题是当输入为空当程序到达空地的点会发生什么stdin,因为在那一刻程序需要另一个输入这是会被清除功能被吃掉.这就是我想要摆脱的东西.(当我可以使用时,fflush(stdin);我才知道,对于我的程序的下一行,stdin无论如何,WILL都是空的,没有问题......)
我正在尝试学习x86_64程序集,我今天正在尝试标准输入输出并偶然发现这个帖子学习程序集 - echo程序名称如何从STDIN读取输入(使用SYSCALL指令)?特别是如果我知道输入将始终是一个整数,我想将其读入寄存器?
编辑: @Daniel Kozar在下面的回答帮助我理解了STDIN和STDOUT如何与Linux上的SYSCALL指令一起工作.我试图编写一个小程序,它从控制台输入中读取一个数字并打印与该数字对应的ascii字符.假如你输入65作为输入,你应该得到A作为输出.还有一个新的线条角色.如果有的话,它可以帮助任何其他人:-)
section .text
global _start
_start:
mov rdi, 0x0 ; file descriptor = stdin = 0
lea rsi, [rsp+8] ; buffer = address to store the bytes read
mov rdx, 0x2 ; number of bytes to read
mov rax, 0x0 ; SYSCALL number for reading from STDIN
syscall ; make the syscall
xor rax, rax ; clear off rax
mov rbx, [rsp+8] ; read the first byte read into rsp+8 by STDIN call …Run Code Online (Sandbox Code Playgroud) 这个问题在我的懒惰中是必要的,因为我有几十个以简单结构执行的脚本:
perl my_script.pl my_input_file
Run Code Online (Sandbox Code Playgroud)
...并将输出打印到stdout.但是,现在我意识到我有某种情况需要将输入输入到这些脚本中.所以,像这样:
perl my_script.pl my_input_file | perl my_next_script.pl | perl third_script.pl > output
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何在不重新编码我的所有脚本以接受stdin而不是用户定义的输入文件的情况下执行此操作?我的脚本通过如下语句查找文件名:
open(INPUT,$ARGV[0]) || die("Can't open the input file");
Run Code Online (Sandbox Code Playgroud)
谢谢你的任何建议!
我正在尝试在其他地方写一个输入的字符串,并且不知道如何取消作为此字符串的一部分出现的新行,我用stdin和fgets获取.
char buffer[100];
memset(buffer, 0, 100);
fgets(buffer, 100, stdin);
printf("buffer is: %s\n stop",buffer);
Run Code Online (Sandbox Code Playgroud)
我试图限制fgets获取的数据量,并限制写入多少数据,但新行仍然存在.我怎样才能简单地将输入的字符串放到最后写的字符中?
在std :: io文档中没有直接接收字符串作为变量的指令,但我认为这应该有效:
use std::io;
let line = io::stdin().lock().lines().unwrap();
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误:
src\main.rs:28:14: 28:23 error: unresolved name `io::stdin`
src\main.rs:28 let line = io::stdin.lock().lines().unwrap();
^~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
为什么?
我正在使用每晚的Rust v1.0.
在此链接指定的编码竞赛中,您需要读取大量数据stdin,进行一些计算并提供大量数据stdout.
在我的基准测试中,尽管我尽可能地尝试优化它,但几乎只有I/O才需要时间.
你输入的是一个字符串(1 <= len <= 100'000)和q行的一对int,其中q也是1 <= q <= 100'000.
我在100倍大的数据集(len = 10M,q = 10M)上对我的代码进行基准测试,这就是结果:
Activity time accumulated
Read text: 0.004 0.004
Read numbers: 0.146 0.150
Parse numbers: 0.200 0.350
Calc answers: 0.001 0.351
Format output: 0.037 0.388
Print output: 0.143 0.531
Run Code Online (Sandbox Code Playgroud)
通过实现我自己的格式化和数字解析内联我设法将时间缩短到使用printf和时间的1/3 scanf.
然而,当我将我的解决方案上传到比赛网页时,我的解决方案需要1.88秒(我认为这是超过22个数据集的总时间).当我看到高分时,有几个实现(在c ++中)在0.05秒内完成,比我快了近40倍!怎么可能?
我想我可以通过使用2个线程来加速它,然后我可以开始计算并写入stdout,同时仍然从stdin读取.然而,这将减少min(0.150, 0.143)我的大型数据集的理论上最佳情况的时间.我仍然没有接近高分...
在下图中,您可以看到消耗时间的统计信息.
该程序由网站编译,具有以下选项:
gcc -g -O2 -std=gnu99 -static my_file.c -lm
Run Code Online (Sandbox Code Playgroud)
和定时这样:
time ./a.out < sample.in > sample.out …Run Code Online (Sandbox Code Playgroud) 我编写了一个命令行工具,以git pull使用python asyncio 执行多个git repos。如果所有存储库均具有ssh无需密码的登录设置,则该方法可以正常工作。如果仅1个回购协议需要输入密码,它也可以正常工作。当多个存储库需要输入密码时,它似乎陷入僵局。
我的实现非常简单。主要逻辑是
utils.exec_async_tasks(
utils.run_async(path, cmds) for path in repos.values())
Run Code Online (Sandbox Code Playgroud)
在这里run_async创建并等待子流程调用,然后exec_async_tasks运行所有任务。
async def run_async(path: str, cmds: List[str]):
"""
Run `cmds` asynchronously in `path` directory
"""
process = await asyncio.create_subprocess_exec(
*cmds, stdout=asyncio.subprocess.PIPE, cwd=path)
stdout, _ = await process.communicate()
stdout and print(stdout.decode())
def exec_async_tasks(tasks: List[Coroutine]):
"""
Execute tasks asynchronously
"""
# TODO: asyncio API is nicer in python 3.7
if platform.system() == 'Windows':
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
else:
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(asyncio.gather(*tasks)) …Run Code Online (Sandbox Code Playgroud)