【问题标题】:How do I recieve "cat" as my programs argumets?如何接收“cat”作为我的程序参数?
【发布时间】:2017-08-18 09:38:44
【问题描述】:

我需要制作一个接收二进制并输出ascii的c程序,

我制作了这个程序,当我将"s o m e t h i n g" 输入到程序参数中,然后程序将其输出为ascii 时,它就可以工作了。

但他们要求我这样做

$cat "file" | ./"program"

当我这样做时,什么也没有发生。

我怎么抓到这只猫?

它不是作为参数发送到我的程序吗?

【问题讨论】:

  • 请阅读How to Askedit你的问题来关注它。
  • 简短回答:man xargs cat file | xargs ./program 是(可能)你想要的。

标签: c binary ascii cat


【解决方案1】:

数据被发送到您程序的标准输入

您可以直接从stdin 阅读。

#include <stdio.h>

int main(void) {
    char line[1000];
    int n = 0;
    while (fgets(line, sizeof line, stdin)) {
        printf("%d - %s", ++n, line); // line includes ENTER
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多