Linux C 按照格式输入的数据,显示SUCCESS最后是成功了还是没成功?
程序代码:
/***************************************************************************/ /** **/ /** c v r t s o i l . c **/ /** **/ /** Converts ASCII soil data file into binary file used by LPJ **/ /** ASCII file has to be in the following format: **/ /** **/ /** Line 1 **/ /** ... **/ /** Line 12 **/ /** lon1,lat1,soilcode1 **/ /** lon2,lat2,soilcode2 **/ /** ... **/ /** **/ /** written by Werner von Bloh, Sibyll Schaphoff **/ /** Potsdam Institute for Climate Impact Research **/ /** PO Box 60 12 03 **/ /** 14412 Potsdam/Germany **/ /** **/ /** Last change: 25.08.2008 **/ /** **/ /***************************************************************************/ #include "lpj.h" #define NSLICE 70000 #define NO_DATA -9999 #define USAGE "Usage: %s soilfile.txt coordfile.bin soilfile.bin\n" typedef struct { Real lon,lat; int soilcode; } Soildata; int main(int argc,char **argv) { FILE *file; Coordfile *coordfile; Coord resol={0.5,0.5}; Coord *coords; int i,j,n,ncoord,nvalid; char soilcode; String line; Soildata *soildata; if(argc!=4) { fprintf(stderr,"Invalid number of arguments.\n" USAGE,argv[0]); return EXIT_FAILURE; } file=fopen(argv[1],"r"); if(file==NULL) { fprintf(stderr,"Error opening file '%s': %s.\n",argv[1],strerror(errno)); return EXIT_FAILURE; } /* skip first 12 lines in ASCII soildata file */ for(i=0;i<12;i++) fgets(line,STRING_LEN,file); n=0; soildata=newvec(Soildata,NSLICE); while(fscanf(file,"%lf,%lf,%d",&soildata[n].lon,&soildata[n].lat,&soildata[n].soilcode)==3) { if(soildata[n].soilcode!=NO_DATA) { n++; if(n % NSLICE==0) { soildata=(Soildata *)realloc(soildata,(n+NSLICE)*sizeof(Soildata)); if(soildata==NULL) { fprintf(stderr,"Error allocating memory for %d soil data.\n",n+NSLICE); return EXIT_FAILURE; } } } } printf("Data read: %d\n",n); fclose(file); coordfile=opencoord(argv[2]); if(coordfile==NULL) { fprintf(stderr,"Error opening file '%s': %s\n",argv[2],strerror(errno)); return EXIT_FAILURE; } ncoord=numcoord(coordfile); coords=newvec(Coord,ncoord); for(i=0;i<ncoord;i++) readcoord(coordfile,coords+i,resol); closecoord(coordfile); file=fopen(argv[3],"wb"); if(file==NULL) { fprintf(stderr,"Error creating file '%s': %s\n",argv[3],strerror(errno)); return EXIT_FAILURE; } nvalid=0; for(i=0;i<ncoord;i++) { soilcode=0; for(j=0;j<n;j++) //if(coords[i].lon==soildata[j].lon && coords[i].lat==soildata[j].lat) if(fabs(coords[i].lon-soildata[j].lon)<0.001 && fabs(coords[i].lat-soildata[j].lat)<0.001) { /*printf("%.2f %.2f %d\n",coords[i].lon,coords[i].lat,soildata[j].soilcode); */ soilcode=(char)soildata[j].soilcode; if(soilcode>0) nvalid++; break; } fwrite(&soilcode,1,1,file); if(j==n) fprintf(stderr,"Soildata not found for cell (%.2f, %.2f)\n", coords[i].lon,coords[i].lat); } printf("Valid soilcodes: %d\n",nvalid); fclose(file); return EXIT_SUCCESS; } /* of 'main' */
上面是程序的源码,运行后显示的是:
[hw@MiWiFi-R2D-srv bin]$ cvrtsoil ~/lpj/input/heihe_soil ~/lpj/input/grid.bin ~/lpj/input/soil.bin Data read: 6 Error opening file '/home/hw/lpj/input/grid.bin': Success这最后由SUCCESS,前面又ERROR了。。有点搞不懂