【问题标题】:Input stream on CC 上的输入流
【发布时间】:2014-03-06 17:19:54
【问题描述】:

所以我很难理解并让我的代码工作。

我有那个代码:

int main(int argc, char *argv[])
{   
    while(1){
        // Buffer containing one sample (left and right, both 16 bit).
        int16_t samples[2];
        unsigned cbBuffer=sizeof(samples);  // size in bytes of 

        // Read one sample from input
        int got=read(STDIN_FILENO, samples, cbBuffer);
        if(got<0){
            fprintf(stderr, "%s: Read from stdin failed, error=%s.", argv[0], strerror(errno));
            exit(1);
        }else if(got==0){
            break;   // end of file
        }else if(got!=cbBuffer){
            fprintf(stderr, "%s: Did not receive expected number of bytes.\n", argv[0]);
            exit(1);
        }

        // Copy one sample to output
        int done=write(STDOUT_FILENO, samples, cbBuffer);
        if(done<0){
            fprintf(stderr, "%s: Write to stdout failed, error=%s.", argv[0], strerror(errno));
            exit(1);
        }else if(done!=cbBuffer){
            fprintf(stderr, "%s: Could not read requested number of bytes from stream.\n", argv[0]);
        }
    }

    return 0;
}

我用那个在终端上调用:

./mp3_file_src.sh bn9th_m4.mp3 | ./passthrough  > /dev/null

我想修改代码,以便他可以取一个数字 n 并处理 n 个样本(所以我在我的代码中得到 samples[2*n])。如果没有为可执行文件提供参数,则使用默认值。

如果我理解正确,我可以验证如果argc&gt;2 是否给出了参数?但是我似乎无法理解如何获取该参数并将其传递到我的代码中。

有什么想法吗?

【问题讨论】:

  • 嗯,基本上你从 argc 变量中得到参数的数量,从 argv 数组中得到参数的值。您必须循环遍历 argv 数组直到 argc 次。

标签: c input stream


【解决方案1】:

argcargv 中的元素数,其中包含给您程序的参数。 argv (argv[0]) 的第一个元素是可执行文件名,因此 argc 始终至少为 1。因此您检查 argc &gt; 2 是否给出了任何参数。

请注意,参数始终是字符串,因此要从中提取数字,您需要使用 strtol 或类似名称。

【讨论】:

    【解决方案2】:

    只需通过argv[1] 访问它。如果是数字参数,则需要将其从字符串转换为数字,例如:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]) {
        int default_opt = 5, option = 0;
    
        if ( argc < 2 ) {
            printf("You didn't supply an argument, using default.\n");
            option = default_opt;
        } else {
            char * endptr;
            option = strtol(argv[1], &endptr, 10);
            if ( *endptr ) {
                printf("You supplied an invalid argument, exiting.\n");
                exit(EXIT_FAILURE);
            }
        }
    
        printf("Option set to %d.\n", option);
    
        return EXIT_SUCCESS;
    }
    

    输出:

    paul@MacBook:~/Documents/src/scratch$ ./arg
    You didn't supply an argument, using default.
    Option set to 5.
    paul@MacBook:~/Documents/src/scratch$ ./arg 7
    Option set to 7.
    paul@MacBook:~/Documents/src/scratch$ ./arg whut
    You supplied an invalid argument, exiting.
    paul@MacBook:~/Documents/src/scratch$
    

    【讨论】:

    • 非常感谢。所以我使用 argv 来获取字符串。但是他不关心 ./mp3_file_src.sh bn9th_m4.mp3 作为参数,因为 |,对吧?仅在 exe 调用之后给出参数?比如说我不想合并两个输入流,那么我必须把它们放在 exe 之后。
    • 您说的是外壳问题,而不是编程问题。当您将输出从mp3_file_src.sh 传送到passthrough 时,passthroughmp3_file_src.sh 一无所知——您的shell 只是将mp3_file_src.sh 的标准输出传送到passthrough 的标准输入。 shell 使用您为passthrough 指定的任何参数调用passthrough,在我知道的每个shell 中,您都可以通过在命令或文件名之后立即指定它们来执行此操作。
    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 2012-09-06
    • 2010-09-29
    • 2010-10-02
    • 2012-11-26
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多