ascii file

Mallikarjun arjunjt at CAOS.IISC.ERNET.IN
Wed Feb 16 04:06:10 EST 2005


Hello Martine Michou,

 Sorry, I thought you wanted to write ASCII data to a file. I did
not know that you wanted to write ASCII vaules to a file in ASCII
format itself. I do have a solution in fact. However you might have gone
through some suggetion that writing a function. That realy works
and it is very simple and can save lot of steps. that goes as follows;

function main(args)
i = 1
while i <= 249
'set t ' i
'd htsgwsfc'
Val=subwrd(result,4)
write (Valuedir.txt,Val, append)
i = i + 1
endwhile

Use this function corresponding to your parameters and file names. You
will have Valuedir.txt generated in current directory. This is realy a
nice solution given by Jean Pierre.

If you can't do this.. you can follow my method also. I will give
you a c-file that converts binary file to ASCII file.. you can try
that also.. Use the following program:

#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");
                 }

Compile this using gcc or cc in linux if in Windows use turbo C or other
compilers. After Compilation use a.out or *.exe as follows;

 a.out 'Binary file name' 'Output file name'
 *.exe 'Binary file name' 'Output file name'

use your file names there without ' '

Ascii file will be created in your current directory..


Try it out.. but I would suggest you follow Jean Pierre's method itself.
It can save time.

Good Luck..



On Tue, 15 Feb 2005, Martine Michou wrote:

> Mallikarjun wrote:
> > Hello Martine Michou,
> >
> > Use 'fwrite' command as follows:
> >
> > 'set fwrite <file name.dat>' *** give file name there 'set gxout
> > fwrite' 'd <variable>'  *** Enter the variable ***
> >
> >
> > To check the .dat file you must quit the grads.
> >
> > Try it out....
> >
>
>
>
> Thanks for your response.
>
> However, I cannot read the file file name.dat with a simple more file
> name.dat or vi file name.dat command.  Apparently the file I generate is
> a binary file and not an ASCII file.
>
> Any suggestion?
>
> Thank you.
>
> Martine Michou
>

--
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