next up previous contents
Next: 7. Manipulating and analysing Up: 6. FORTRAN style interpreter Previous: 6.8 Macros

   
6.9 Working with files

The command language offers the user several commands to write variables to a file or read values from a file. First a file needs to be opened using the command 'fopen'. An optional parameter 'append' allows one to append data to an existing file. Once the task is finished, the file must be closed via 'fclose'. Only one file can be open at any given time. The commands 'fget' and 'fput' are used to read and write data, respectively. The following example illustates the usage of these commands:

      1  fopen sin.dat
      2  fput 'Cool sinus function'
      3  #
      4  do i[1]=1,50
      5    r[1]=i[1]*0.1
      6    r[2]=sin(r[1])
      7    fput r[1],r[2]
      8  enddo
      9  #
     10  fclose

In line 1 we open the file 'sin.dat' and write a title (line 2). If the file already exists it will be overwritten. Note that the text must be given in single quotes. Text and variables may be mixed in a single line. Next we have a loop calculatin $y=\sin(x)$ and writing the resulting x and y values to the open file (line 7). Finally the file is closed (line 10). To read values from a file use simply the command 'fget' and the read numbers will be stored in the specified variables. In contrast to writing to a file, mixing of text and number is not allowed when reading data. However, complete lines will be skipped when the command 'fget' is entered without any parameters.


next up previous contents
Next: 7. Manipulating and analysing Up: 6. FORTRAN style interpreter Previous: 6.8 Macros
Thomas Proffen {Billinge}
1999-06-26