【发布时间】:2016-03-01 21:38:07
【问题描述】:
我想实现一个scanf(),我找到了一个implementation(我将它命名为new_scanf()),它使用fgets()从vfscanf()中的特定流中获取输入。
这是我的测试程序,
int main() {
int a, b;
new_scanf("%d", &a);
new_scanf("%d", &b);
printf("a=%d b=%d", a, b);
}
这个程序在我输入时运行
123 <enter>
456 <enter>
a=123 b=456 ;program shows result correctly
但是我打字的时候出了点问题
123 456 <enter>
;the program is stuck here waiting for another input. scanf() should
不要被困在这里
new_scanf() 的这种行为与 glibc 的scanf() 不同,我不知道如何解决,谁能告诉我如何完美地模拟scanf()?
【问题讨论】:
-
请在问题本身中发布minimal reproducible example。
-
你了解
new_scanf的实现吗?如果没有,具体是什么你不明白?您是否尝试过以任何方式调试问题? -
@kaylum 如果我理解正确,他写了
new_scanf -
@tobi303 我的理解是 OP 将
new_scanf从 Internet 上删除。 OP与此有关。所以我在问OP是否真的阅读了代码并试图理解和调试它。 -
C 和 C++ 是不同的语言。这看起来像 C。如果您编译为 C++,请将标记更改为 C++,但不能同时使用两者。