c - 如何从标准输入打印行?

标签 c while-loop scanf

我将标准输入重定向为在命令行上运行时从命令 ls -l 生成的输出,并且我想打印出其结果。

所以说,在运行 ls -l 后,我得到以下输出:

total 519
-rw-------  1 jeff  dev    1274 22 Jun 14:52 prinprog
-rwx---xr--  1 jeff  dev   2410  6 Apr 12:10 temp.txt
-rwxr----x  1 jeff  dev    8128  1 Feb  2013 yyz
-rw-r--r--  1 jeff  dev      98 15 Feb  2013 yyz.c

现在,编译我的 C 程序后,我执行 ls -l | ./testing 和我有效地将标准输入从键盘更改为 ls -l 命令生成的输出。现在我想要在 C 程序中执行的操作是打印 ls -l 命令生成的输出。

我通过 scanf() 和 while 循环来做到这一点。我遇到的问题是格式化我的文本。第一行“total 519”对我来说有点无用,但它让我失望。

#include <stdio.h>

int main() {

char permissions[10];
int num;
char user[20];
char random[20];
int fileSize;

while(scanf("%s %d %s %s %d", permissions, &num, user, random, &fileSize) != EOF) {
    printf("%s %d %s %s %d\n", permissions, num, user, random, fileSize);
}

return 0;  
} 

最佳答案

建议:

#include <stdio.h>

int main() {
  static char line[256];

  while (fgets(line, sizeof(line), stdin) != NULL) {
    char perms[11];
    int nlinks;
    char user[20];
    char grp[20];
    int size;
    char month[10];
    int day;
    char time[10];
    char name[256];

   if (sscanf(line, "%10s %d %19s %19s %d %9s %d %9s %255s",
               perms, &nlinks, user, grp, &size, month, &day, time, name) == 9)
       printf("perms=%s nlinks=%d user=%s grp=%s size=%d month=%s date=%d time/year=%s name=%s\n",
         perms, nlinks, user, grp, size, month, day, time, name);
  }

  return 0;  
} 

执行示例:

ls -l /usr/sbin | ./a.out
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=9788 month=déc. date=13 time/year=2016 name=accessdb
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=3078 month=oct. date=5 time/year=22:43 name=addgnupghome
perms=lrwxrwxrwx nlinks=1 user=root grp=root size=7 month=juin date=27 time/year=2016 name=addgroup
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=860 month=avril date=2 time/year=2017 name=add-shell
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=34509 month=juin date=27 time/year=2016 name=adduser
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=4136 month=janv. date=23 time/year=2017 name=alsabat-test
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=83912 month=janv. date=23 time/year=2017 name=alsactl
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=27872 month=janv. date=23 time/year=2017 name=alsa-info
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=2219 month=oct. date=5 time/year=22:43 name=applygnupgdefaults
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=44224 month=déc. date=26 time/year=2016 name=arp
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=42920 month=nov. date=24 time/year=2017 name=arpd
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=13542 month=oct. date=10 time/year=2016 name=aspell-autobuildhash
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=17884 month=déc. date=8 time/year=2016 name=atd
perms=-rwxr-xr-x nlinks=1 user=root grp=root size=105136 month=janv. date=23 time/year=2017 name=avahi-daemon
...

关于c - 如何从标准输入打印行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54272007/

相关文章:

在 C 中从 int 创建 char 数组

c - fscanf 无法读取我在 C 中输入的文件的第一个整数,但读取其余部分

c++ - 从 Hex 中的 istream 中读取 double

c - 日期比较以查找 C 中哪个更大

c - 在自制操作系统中使用库函数

c - 强制 FreeBSD 应用程序生成核心文件

java - 在 while 循环中使用扫描仪时,如何 "prime"循环并创建哨兵?

c - 在 C 中混合 'switch' 和 'while'

java - while 循环给出空异常指针并且不会进入

c - 如何在附加后打印文件中的值