当 scanf!=EOF 或 scanf==1 时?

发布于 2024-10-02 04:39:19 字数 144 浏览 2 评论 0原文

其他条件相同(格式良好的数据、良好的缓冲实践等等),是否有理由让我更喜欢在 scanf 的返回值为 1 时进行循环,而不是 !EOF?我可能在某处读过这篇文章,或者其他什么,但我也可能有错。其他人怎么看?

Ceteris paribus (well formed data, good buffering practices and what not), is there a reason why I prefer to loop while the return of scanf is 1, rather than !EOF? I may have read this somewhere, or whatever, but I may have it wrong as well. What do other people think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高跟鞋的旋律 2024-10-09 04:39:19

scanf 返回成功转换的项目数...或 EOF(出错时)。因此,请按照有意义的方式对条件进行编码。

scanfresult = scanf(...);
while (scanfresult != EOF) /* while scanf didn't error */
while (scanfresult == 1) /* while scanf performed 1 assignment */
while (scanfresult > 2) /* while scanf performed 3 or more assignments */

人为的示例

scanfresult = scanf("%d", &a);
/* type "forty two" */
if (scanfresult != EOF) /* not scanf error; runs, but `a` hasn't been assigned */;
if (scanfresult != 1) /* `a` hasn't been assigned */;

编辑:添加了另一个更人为的示例

int a[5], b[5];
printf("Enter up to 5 pairs of numbers\n");
scanfresult = scanf("%d%d%d%d%d%d%d%d%d%d", a+0,b+0,a+1,b+1,a+2,b+2,a+3,b+3,a+4,b+4);
switch (scanfresult) {
case EOF: assert(0 && "this didn't happen"); break;
case 1: case 3: case 5: case 7: case 9:
    printf("I said **pairs of numbers**\n");
    break;
case 0:
    printf("What am I supposed to do with no numbers?\n");
    break;
default:
    pairs = scanfresult / 2;
    dealwithpairs(a, b, pairs);
    break;
}

scanf returns the number of items succesfully converted ... or EOF on error. So code the condition the way it makes sense.

scanfresult = scanf(...);
while (scanfresult != EOF) /* while scanf didn't error */
while (scanfresult == 1) /* while scanf performed 1 assignment */
while (scanfresult > 2) /* while scanf performed 3 or more assignments */

Contrived example

scanfresult = scanf("%d", &a);
/* type "forty two" */
if (scanfresult != EOF) /* not scanf error; runs, but `a` hasn't been assigned */;
if (scanfresult != 1) /* `a` hasn't been assigned */;

Edit: added another more contrived example

int a[5], b[5];
printf("Enter up to 5 pairs of numbers\n");
scanfresult = scanf("%d%d%d%d%d%d%d%d%d%d", a+0,b+0,a+1,b+1,a+2,b+2,a+3,b+3,a+4,b+4);
switch (scanfresult) {
case EOF: assert(0 && "this didn't happen"); break;
case 1: case 3: case 5: case 7: case 9:
    printf("I said **pairs of numbers**\n");
    break;
case 0:
    printf("What am I supposed to do with no numbers?\n");
    break;
default:
    pairs = scanfresult / 2;
    dealwithpairs(a, b, pairs);
    break;
}
浪菊怪哟 2024-10-09 04:39:19

取决于您想要如何处理格式错误的输入 - 如果您的扫描模式不匹配,您可能会返回 0。因此,如果您在循环之外处理这种情况(例如,如果您将其视为输入错误),则与 1 进行比较(或者无论您的 scanf 调用中有多少项)。

Depends what you want to do with malformed input - if your scan pattern isn't matched, you can get 0 returned. So if you handle that case outside the loop (for example if you treat it the same as an input error), then compare with 1 (or however many items there are in your scanf call).

在风中等你 2024-10-09 04:39:19

来自 http://www.cplusplus.com/reference/clibrary/cstdio/scanf/< /a>

成功时,该函数返回
成功读取的项目数。这
count 可以匹配预期的数量
读数或更少,甚至为零,如果
出现匹配失败的情况。在这种情况下
任何数据之前输入失败
可以成功读取,EOF为
返回。

确保您读取预期项目数的唯一方法是将返回值与该数字进行比较。

From http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

On success, the function returns the
number of items succesfully read. This
count can match the expected number of
readings or fewer, even zero, if a
matching failure happens. In the case
of an input failure before any data
could be successfully read, EOF is
returned.

The only way to be sure that you read the number of items intended is to compare the return value to that number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文