1
我使用以下代碼中的行讀取:停止當EOF在線的Python
for line in sys.stdin:
print('Output: ', end='')
print(line.rstrip('\n'))
作爲輸入我看到輸出如:
test
Output: test
another test
Output: another test
eof at end of line^DOutput: eof at end of line
I can still type here
Output: I can still type here
...
在這種情況下^D
裝置鍵入CTRL- D或EOF字符。
我的程序應該在一個EOF發生在一行的結尾而不是一行中唯一的字符時結束。我怎樣才能得到這種行爲?
實例行爲:
test
Output: test
another test
Output: another test
eof at end of line^DOutput: eof at end of line
[program terminates]
終端驅動程序不關閉流,它始終解釋'^ D'。但它只是讓'read(2)'返回,它只返回0時暗示EOF。 –
有什麼方法可以實現我想要的行爲?我知道在C中,我可以使用像'while(getline(&line,&n,stdin)!= -1)'這會產生我預期的行爲。 – carloabelli