c - argv[2] value not passing properly from cmd -
i'm having problem code :
#include <stdio.h> #include <stdlib.h> #include <string.h> #define n 20 int main( int argc , char * argv[]) { int giorno , mese , anno , metri , primo_g = 31, primo_m = 12, primo_a = 3000, = 0 ; char tipo , destinazione [n+1] ; float km_arrivo = 0 , costo_tot = 0 ; file * fdati; if(argc != 2) { fprintf(stderr ,"errore nella linea di comando\n"); exit (exit_failure); } if ((fdati = fopen ("viaggi.dat", "r")) == null ) { fprintf(stderr ,"errore nell'apertura del file\n"); exit (exit_failure); } while ((fscanf(fdati , "%s %d/%d/%d %d %c" , destinazione , &giorno , &mese , &anno , &metri , &tipo))!= eof){ if (strcmp (argv[2],destinazione) == 0 ) { if((tipo == 'r')||(tipo =='r')){ i++; km_arrivo = km_arrivo + (float)(metri/1000) ; costo_tot = (i*5) + 0.10 * (km_arrivo); } } if ((anno<primo_a)||((anno==primo_a)&&(mese<primo_m))||((anno==primo_a)&&(mese==primo_m)&&(giorno<primo_g))){ primo_a= anno; primo_m = mese; primo_g = giorno; } } if (fclose(fdati) != 0 ){ fprintf(stderr , "errore nella chiusura del file\n"); exit(exit_failure); } printf("chilometri da %s: %.2f\n", argv[2] , km_arrivo); printf("data primo viaggio : %d/%d/%d\n", primo_g , primo_m , primo_a); printf("il costo totale dei viaggia da torino : €%.2f", costo_tot); exit(exit_success); }
viaggi.dat contains :
torino 22/01/2011 120000 milano 01/03/2011 80000 r genova 03/06/2011 100000 bologna 01/02/2011 100000 torino 15/01/2011 120000 r milano 20/01/2011 80000 r torino 01/01/2011 80000 torino 22/01/2011 80000 r
the problem when run program cmd , write value of argv[2] (e.g "torino") tells me program stoped working . if run program without writing value of argv[2] runs , stderr message appears.
thank in advance,
regards.
edit : changed argv[2] argv[1] i'm still having same error.
i'm running cmd , :
c:\users\hp g6\desktop\fasdb\bin\debug>fasdb.exe torino
remember argc
number of entries in argv
array, , if argc
2
have indexes 0
, 1
in argv
array. use argv[2]
argc
must 3
.
argv[argc]
always null
.
Comments
Post a Comment