I have the following program called 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);
}
in which I have a .dat file intended to be used for the args in this program, however if I type:
cat testscandata.dat | ./ScorecommandLine
the program does not read the file in as the parameters for the program. How do I solve this?
Thanks
You are confusing two different ways of passing input into a program. You can pass arguments to
mainin a program by invoking the command from the command-line and listing the arguments. For example:These arguments will be passed into
mainthroughargv.You can also pipe input into a program by sending in data through
stdinby using pipes and redirection:This will take the output of
SomeCommandand use that as thestdinstream inScoreCommandLine. You can read it by usingscanf, etc.In your case, you should either rewrite the program so that you aren’t expecting all the arguments to be passed in through the command-line, or you should use the
xargsutility to convertstdininto command-line arguments:Hope this helps!