help

Mallikarjun arjunjt at CAOS.IISC.ERNET.IN
Sat Feb 5 11:34:17 EST 2005


Hello anshu,

  Here is the debugged code.

#include<stdio.h>
main(int argc, char *argv[])
{ FILE *fp,*np;
  float a;
  int i;
    if(argc!=3)
  {puts("Your command should be only three arguments");
          exit(1);
  }

  fp=fopen(argv[1],"r");
  np=fopen(argv[2],"wb");
  if(fp==NULL && np==NULL)
  {  puts("Invalid Path or The File Doesn't Exist\n");
     exit(1);
  }
while((fscanf(fp,"%f",&a)!=EOF))
 fwrite(&a,sizeof(float),1,np);
   fclose(fp);
   fclose(np);
   printf("The BINARY file created\n");
                 }


 You first copy and paste this program to <file name>.c file and then
compile it using c compiler cc or gcc.

 cc -g <file name>.c

now executable file will be created in your current directory.
Now use this command:

a.out <ascii file name>  <output filename>.grd

Now your binary file(Grads readable) will be dumped in current directory
with proper size.
it will be <no of ascii values> * 4 = size of grd file.

If you want to convert same binary file to ascii values as before
use the following code and follow the steps as above:

#include<stdio.h>
main(int argc, char *argv[])
{ FILE *fp,*np;
  float a;
  int i;
  if(argc!=3)
  {puts("You should give three arguments");
   exit(1);
  }
  fp=fopen(argv[1],"rb");
  np=fopen(argv[2],"w");
  if(fp==NULL && np==NULL)
  {  puts("Invalid Path or The File Doesn't Exist\n");
     exit(1);
   }
while((fread(&a,sizeof(float),1,fp))!=0)
   fprintf(np,"%f\n",a);
   fclose(fp);
   fclose(np);
   printf("The ASCII file created\n");
                 }



SYNTAX : a.out <binary file name> <output file name>


Follow above instructions, your work will be done.

Best Wishes,


--
Mallikarjuna.K
J.R.F
Centre for Atmospheric and Oceanic Sciences
Indian Institute of Science
Bangalore - 560 012



More information about the gradsusr mailing list