更新时间:2023-11-17 23:26:22
我建议使用
while(!feof(stdin) && scanf ( "%d %d ", &a, &b) == 2)
实际上***测试 feof after(不是之前!)一些输入操作,所以:
and actually it is better to test feof after (not before!) some input operation, so:
while (scanf("%d %d ", &a, &b) == 2 && !feof(stdin))
顺便说一句,在许多系统上,stdin
是 line 缓冲的,至少在交互式终端上是这样(但当 stdin 是 pipe(7)),参见 setvbuf(3)
BTW, on many systems stdin
is line buffered, at least with interactive terminals (but perhaps not when stdin is a pipe(7)), see setvbuf(3)
在 Linux 上 &POSIX 您可能会考虑使用 getline(3) (甚至 readline(3) 如果从终端读取,因为 readline
提供了编辑能力),然后用例如解析该行sscanf(3)(也许也使用 %n
) 或 strtol(3)
On Linux & POSIX you might consider reading every line with getline(3) (or even with readline(3) if reading from the terminal, since readline
offers editing abilities), then parsing that line with e.g. sscanf(3) (perhaps also using %n
) or strtol(3)