0

と呼ばれる次のプログラムがありますScorecommandline

int main (int argc, char *argv[]) {
    if (argc!=15) {
        usage();
        exit(1);
    }

    int iArray[14];
    int i = 0;
    while(1){
        if(scanf("%d",&iArray[i]) != 1){
        break;
        }
        i++;
        if(i == 14) {
        i = 0;
        }
    }

    int age = atoi(iArray[1]);
    int b_AF = atoi(iArray[2]);
    int b_ra = atoi(iArray[3]);
    int b_renal = atoi(iArray[4]);
    int b_treatedhyp = atoi(iArray[5]);
    int b_type2 = atoi(iArray[6]);
    double bmi = atof(iArray[7]);
    int ethrisk = atoi(iArray[8]);
    int fh_cvd = atoi(iArray[9]);
    double rati = atof(iArray[10]);
    double sbp = atof(iArray[11]);
    int smoke_cat = atoi(iArray[12]);
    int surv = atoi(iArray[13]);
    double town = atof(iArray[14]);

    double score = cvd_femal(age,b_AF,b_ra,b_renal,b_treatedhyp,b_type2,bmi,ethrisk,fh_cvd,rati,sbp,smoke_cat,surv,town,&error,errorBuf,sizeof(errorBuf));
    if (error) {
        printf("%s", errorBuf);
        exit(1);
    }
    printf("%f\n", score);
}

このプログラムの引数に使用することを目的とした.datファイルがありますが、次のように入力すると:

cat testscandata.dat | ./ScorecommandLine

プログラムは、プログラムのパラメーターとしてファイルを読み取りません。これを解決するにはどうすればよいですか?

ありがとう

4

2 に答える 2

4

入力をプログラムに渡す 2 つの異なる方法を混同しています。mainコマンドラインからコマンドを呼び出して引数をリストすることにより、プログラムに引数を渡すことができます。例えば:

./ScoreCommandLine 1 2 3 4 5 6 7 8 9 10 11 12 13 14

mainこれらの引数はthroughに渡されますargv

stdinパイプとリダイレクトを使用してデータを送信することにより、入力をプログラムにパイプすることもできます。

SomeCommand | ./ScoreCommandLine

これは の出力を受け取り、それを のストリームSomeCommandとして使用します。などで読めます。stdinScoreCommandLinescanf

あなたの場合、すべての引数がコマンドライン経由で渡されることを期待しないようにプログラムを書き直すか、xargsユーティリティを使用stdinしてコマンドライン引数に変換する必要があります。

xargs ./ScoreCommandLine < testscandata.dat

お役に立てれば!

于 2013-02-07T23:53:38.793 に答える
1

これはプログラムに引数として渡されませんが、プログラムにパイプされstdinます。同様の関数./ScorecommandLineを介して読み取ることができますが、コマンドライン引数としてではありません。scanf

ファイル (または ) を読み取る新しいスクリプトを作成しstdin、それを実行可能な引数として渡す別のプログラムを実行する必要があります。

あなたのプログラムを調べた後、 を使っif (argc!=15)て読んstdinでいてscanf、コマンドライン引数をどこにも解析していないので、 を削除することをお勧めします。

于 2013-02-07T23:51:35.020 に答える