Program2

问题描述 投票:0回答:1
I wanna use the output of the first program as input(stdin) for the program2 to do some calculations.

for now this is what i have in program 2

program 1 just creates several numbers per row. I want to use those numbers in program 2 to double them and print the result.

What am I missing here?

int main(int argc, char *argv[]) {

    char userInput[100];
    int num[100];

    FILE *cmdLn = stdin;

    if (argc > 2) {
        fprintf(stderr, "Usage: %s [<file>]\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (argc == 2) {

        cmdLn = fopen(argv[1], "r");

        if (!cmdLn) {
            perror(argv[0]);
            exit(EXIT_FAILURE);
        }
    }

    int numInput[100];

    for (int i = 0; i < 100; i++) {
        fscanf(cmdLn, "%d", &numInput[i]);
        printf("%d\n", 2*numInput[i]);
    }


    if (cmdLn != stdin) {
        fclose(cmdLn);
    }
    exit(EXIT_SUCCESS);
}

I wanna do this: Progam1
c windows pipe stdin
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.