scanf 终止标准输入 [英] scanf terminating standard input

查看:61
本文介绍了scanf 终止标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>

int main(){

    char str[20];
    int number;
    while(2 == scanf("%s  %*d %*s  %*s    %d %*d %*s %*s %*s", str, &number)){
        printf("%s\n",str);
        printf("%d\n", number);

    }
}

我试图从标准输入中读取多行,所有这些都具有相同的格式.我当前的实现有效,但我似乎无法弄清楚一旦用户没有输入一行然后按回车键就终止输入.该程序一直在等待用户的输入.无论如何使用 scanf 和我当前的实现来做到这一点?

I am trying to read multiple lines from standard input all of which have the same format. My current implementation works but I can't seem to figure out anyway to terminate the input once the user does not input a line and then presses enter. The program keeps expecting input from the user. Is there anyway to do this using scanf and with my current implementation?

注意:我正在运行如下命令:

NB: I am running commands like:

ls -l | my_program

这并没有给我任何输出.

and that is not giving me any output.

推荐答案

关于仅使用 scanf() 读取 ls -l 命令的输出是这样的:

About the simplest modification using only scanf() to read the output from ls -l command is this:

#include <stdio.h>

int main(void)
{
    char str[20];
    int number;
    if (scanf("%*[^\n]") != 0)
        return 1;
    while (2 == scanf("%s %*d %*s %*s %d %*[^\n]", str, &number))
        printf("%s  %d\n", str, number);
    return 0;
}

第一个 scanf() 读到但不包括换行符;跳过 ls -l 输出的 total NNN 行.第二个 scanf() 与你的几乎相同,除了它跳过第二个数字(文件大小)之后的数据直到换行符.幸运的是,开头的 %s 会跳过前导空格,包括换行符,因此扫描集 %*[^\n] 将换行符留在后面的事实不会'一点都不重要.

The first scanf() read up to but not including the newline; that skips the total NNN line that ls -l outputs. The second scanf() is almost the same as yours except that it skips the data after the second number (the file size) up to the newline. Fortunately, the %s at the start skips leading white space, including newlines, so the fact that the scan sets %*[^\n] leave the newline behind doesn't matter in the slightest.

我的目录收益:

$ ls -l
total 152
-rw-r--r--    1 jleffler  staff  22072 Dec 30 09:19 LICENSE.md
-rw-r--r--    1 jleffler  staff   2694 Dec 30 09:19 README.md
dr-xr-xr-x    4 jleffler  staff    128 Aug 14  2016 Safe
drwxr-xr-x   84 jleffler  staff   2688 Jan 12 00:58 Untracked
drwxr-xr-x   26 jleffler  staff    832 Dec 25 22:39 bin
-rw-r--r--    1 jleffler  staff   1875 Jan 19 00:08 crseq71.sql
drwxr-xr-x   14 jleffler  staff    448 Dec 30 09:19 doc
drwxr-xr-x   10 jleffler  staff    320 Jan 12 01:13 etc
-rw-r--r--    1 jleffler  staff    173 Mar  3  2017 get.jl.activity
drwxr-xr-x   21 jleffler  staff    672 Jan  7 23:02 inc
drwxr-xr-x    5 jleffler  staff    160 May 28  2017 lib
-rw-r--r--    1 jleffler  staff    390 Jun 21  2017 makefile
drwxr-xr-x    4 jleffler  staff    128 Jan 12 01:13 packages
-rw-r--r--    1 jleffler  staff    218 Oct 15 10:18 pending.20171015.101828
-rwxr-xr-x    1 jleffler  staff   8704 Jan 19 21:39 rl43
-rw-r--r--    1 jleffler  staff    248 Jan 19 21:39 rl43.c
drwxr-xr-x    3 jleffler  staff     96 Jan 19 21:21 rl43.dSYM
-rw-r--r--    1 jleffler  staff   2247 Jan  6 22:44 sll43.c
-rw-r--r--    1 jleffler  staff    126 Oct 24 12:52 so-4689-5145.info
drwxr-xr-x  227 jleffler  staff   7264 Jan 19 11:34 src
-rw-r--r--    1 jleffler  staff     92 Jan 19 21:20 testfile.txt
-rw-r--r--    1 jleffler  staff    645 Jan 18 23:37 union71.c
$ ls -l | ./rl43
-rw-r--r--  22072
-rw-r--r--  2694
dr-xr-xr-x  128
drwxr-xr-x  2688
drwxr-xr-x  832
-rw-r--r--  1875
drwxr-xr-x  448
drwxr-xr-x  320
-rw-r--r--  173
drwxr-xr-x  672
drwxr-xr-x  160
-rw-r--r--  390
drwxr-xr-x  128
-rw-r--r--  218
-rwxr-xr-x  8704
-rw-r--r--  248
drwxr-xr-x  96
-rw-r--r--  2247
-rw-r--r--  126
drwxr-xr-x  7264
-rw-r--r--  92
-rw-r--r--  645
$

这篇关于scanf 终止标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆