# File Input and Output

Shows

- how to store and retrieve data from a file,
- how to do primitive file input and output
- how to find files in the EULER command line.

## Numerical Data from Files

Often, you have numbers stored in files in a unpredictable format. You need to open the file for reading using

        >open("test.dat","r")

Then you can use

        >getvector(N)

to get a vector of these numbers. The function will read over any non-numeric data and will stop after reading N numbers. You can use

        >{v,M}=getvector(N)

to get the vector v and the actually read number of data M. If the vector is a matrix, you will have to resize it with

        >A=redim(v,[n,m])

There is also the utility function

        >A=getmatrix(n,m,filename)

which will do all work for you. If the filename is empty, the function will assume that you already opened a file for reading. Otherwise, it will open and close the file for you. This way, you can read several matrices in a single file. The converse is

        >writematrix(A,filename)

If the filename is not empty, the file will be created (beware!) and closed. Otherwise, you need to open and close the file yourself.

## Primitive Input and Output

There are some primitive file input and output routines.

        >open("test.dat","w")

will open the file test.dat for writing, erasing it if it exists. The two parameters of open must be strings and work just like fopen. So

        >open("test.dat","r")

opens the file for reading. Opening in binary mode can be achieved with "wb" or "rb". "a" stand for append and will write to the end of the file. You can only open a single file at any time. Opening a second one will close the first one.

        >close()

will close the file again.

        >putchar(c)

puts a character with code c to a binary file. If c is a 1xn real vector, it will put n characters to the file.

        >putword(x)
        >putlongword(x)

Puts a word or a vector of words (long words) to the file in binary format.

        >c=getchar()

reads a character.

        >v=getchar(n)

reads a vector of n characters.

        >x=getword()
        >x=getlongword()

reads a word or long word.

        >x=getword(n)
        >x=getlongword(n)

reads n words (long words) from a binary file.

        >s=getstring(n)

reads a string of length n from a binary file.

You can check for the end of the file with

        >eof()

It will return 1, if the file is completely read.

        >write("string")

is a function, which writes a string to a text file. You can add a newline with

        >write("string"|asc(10))

## Directories

The active directory can be changed with

        >cd "path"

where path is the new directory and may include a drive letter, like

        >cd "a:\progs"

The changedir function does the same. It will return the active directory when it is called with "".

The command

        >dir "*.e"

displays all files in the active directory, which fit with the pattern. An empty string fits all files.

The function

        >searchfile("*.e");

returns the first file that fits to the pattern. Further calls of searchfile without any parameters return further files until "" indicates that there are no more fits.