【问题标题】:Why can't I print the first argument passed to my program?为什么我不能打印传递给我的程序的第一个参数?
【发布时间】:2014-02-28 16:05:27
【问题描述】:

我在c中做了一个简单的代码:

#include <stdio.h>
int main( int argc, char* argv[] ) {
    printf( "Hello, just wanted to say: %s.\n", argv[1] );
    return 0;
}

并从中制作了两个程序 - app1app2

现在,我正在试验管道:

./app1 Bye | ./app2

输入%s 的输出是(null),而不是预期的“Hello”。为什么?

【问题讨论】:

  • 您看到来自./app2 的输出./app1 的输出与您的示例命令无关

标签: c bash arguments


【解决方案1】:

您似乎误解了管道的工作原理。当您像以前一样使用管道时,app1 的输出将用作stdinapp2,而不是app2 的命令行参数。

是这样的:

stdin->app1->stdout >----------> stdin->app2->stdout
                        pipe

【讨论】:

    猜你喜欢
    • 2016-02-01
    • 2020-07-05
    • 2021-12-29
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    相关资源
    最近更新 更多