// MakeChannelState.c : Defines the entry point for the console application. // original .cpp program written by Charlie Luce // converted to .c by Nathalie Voisin // #include #include #include #include #define nclass 9 //argv1 is the stream network file //argv2 is the stream class file //argv3 is the initial depth //argv4 is the output date string - MM.DD.YYYY.hh.mm.ss //Example call MakeChannelState stream.network.dat stream.class.dat 0.25 10.01.1990.03.00.00 int main(int argc, char** argv) { //to work with data from command line float depth; char ofname[256]="Channel.State."; //Data that will be read in from class file char dumhd[256]; int sclass; float w,d,inf,n,width[nclass+1]; //Data that will be read in from network file int segn, ord; float slope, length; //calculated variables float volume; // cpp to c FILE *fp,*fp2; char cjunk[1000]; int dest; if (argc < 5){ fprintf(stderr,"ERROR: Insufficient arguments\n"); fprintf(stderr,"Usage: MakeChannelState \n"); exit(0); } // convert character constant depth to real value depth = atof(argv[3]); fprintf(stdout,"Initial depth set to %f\n",depth) ; //Read in channel class information fp-NULL; fp=fopen(argv[2],"r"); if ( fp==NULL) { fprintf(stderr,"Error opening %s\n",argv[2]); exit(0); } //fscanf(fp,"%s \n",&dumhd); fgets(cjunk,1000,fp); fprintf(stdout,"Reading %s in %s\n",cjunk,argv[2]); while ( !feof(fp) ) { if ( fscanf(fp,"%d %f %f %f %f\n",&sclass,&w,&d,&n,&inf) !=5) { fprintf(stderr,"Error reading in %s\n",argv[2]); exit(0); } width[sclass] = w; } fclose(fp); fp=NULL; //Open stream network file, read in lengths and segments, calc volumes and output strcat(ofname,argv[4]); fprintf(stdout,"Open %s for writing\n",ofname); fp=fopen(ofname,"w"); if ( fp==NULL) { fprintf(stderr,"Error opening %s\n",ofname); exit(0); } fprintf(stdout,"Open %s for reading\n",argv[1]); fp2=fopen(argv[1],"r"); if ( fp2==NULL) { fprintf(stderr,"Error opening network file %s\n",argv[1]); exit(0); } while (!feof(fp2) && fscanf(fp2,"%d %d %f %f %d %d", &segn,&ord,&slope,&length,&sclass,&dest)==6) { // read end of line fgets(cjunk,1000,fp2); volume = length * depth * width[sclass]; fprintf(fp,"%d %f\n",segn,volume); } fclose(fp); fclose(fp2); return 0; }