/**************** data_read.c by Michael J. Scott April 17, 1997 for ME171 reading the file in.dat ***********/ /**************** I changed this file a little bit from the original one to make it work well, so it's not the same as the handout. Also note that this code is intended to be illustrative, not garanteed bug_free. -----Lin. **************/ #include #include #include #define N 4 #define MAXVECTORS 3000 /* more vectors than we can imagine wanting to read alternatively (and better), use dynamic allocation. Not required, however */ void main(){ FILE *fpin,*fpout; int i,j,vectors; char oneline[80]; double vec[MAXVECTORS][N]; /******* read in the data file ********/ fpin = fopen("in.dat","r"); vectors=0; while(fgets(oneline,80,fpin)){ /* as long as there are lines to read */ if(strchr(oneline,'&')){ /* if it's an ampersand, notice */ printf("Got an ampersand.\n"); vectors--; } else { if(sscanf(oneline,"%lf %lf %lf",&vec[vectors][0],&vec[vectors][1], &vec[vectors][2])!=3){ /* ignore lines that don't have three doubles */ printf("oops\n"); vectors--; } } vec[vectors][3]=1.; vectors++; /* keep track of how many vectors we've read. note you have to decide what to do with the &'s; */ } fclose(fpin); printf("%d\n",vectors); /******* write the output file *******/ fpout = fopen("out.dat","w"); for(i=0;i