C上的输入流

C上的输入流,c,input,stream,C,Input,Stream,因此,我很难理解并让我的代码正常工作 我有这样的密码: 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

因此,我很难理解并让我的代码正常工作

我有这样的密码:

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;
}
我想修改代码,这样他就可以获取n个数字并处理n个样本(因此我在代码中得到
samples[2*n]
)。如果没有为可执行文件提供参数,则使用默认值

如果我理解正确,我可以验证在
argc>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;
}

argc
argv
中的元素数,其中包含给定给程序的参数。
argv
argv[0]
)的第一个元素是可执行文件名,因此
argc
始终至少为1。因此,您可以检查
argc>2
以查看是否提供了任何参数


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

基本上,您可以从argc变量中获取参数的数量,从argv数组中获取参数的值。你必须在argv阵列中循环,直到argc次。非常感谢。所以我使用,argv来获取字符串。但他不在乎./mp3_file_src.sh bn9th_m4.mp3作为参数,因为|,对吧?参数仅在exe调用后给出?比如说,我不想合并两个输入流,然后我必须把它们放在exe之后。你说的是一个shell问题,而不是编程问题。当您将输出从
mp3\u文件\u src.sh
传输到
passthrough
时,
passthrough
mp3\u文件\u src.sh
一无所知-您的shell只是将
mp3\u文件\u src.sh
的标准输出传输到
passthrough
的标准输入。shell使用您为
passthrough
指定的任何参数调用
passthrough
,我知道在每个shell中,您都是通过在命令或文件名之后立即指定这些参数来实现的。
#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$