1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<META NAME="VPSiteProject" CONTENT="file:///E|/euler/docs/Euler.vpp">
<META NAME="GENERATOR" Content="Visual Page 2.0 for Windows">
<TITLE>Files and Directories</TITLE>
<BASE target="_self">
<LINK REL="stylesheet" TYPE="text/css" HREF="euler.css">
</HEAD>
<BODY>
<H1 ALIGN="CENTER">File Input and Output</H1>
<P>Shows</P>
<UL>
<LI><A HREF="#Numerical Data from Files">how to store and retrieve data from a file</A>,
<LI><A HREF="#Primitive Input and Output">how to do primitive file input and output</A>
<LI><A HREF="#Directories">how to find files in the EULER command line</A>.
</UL>
<P>
<PRE></PRE>
<H2 ALIGN="CENTER"><A NAME="Numerical Data from Files"></A>Numerical Data from Files</H2>
<P>Often, you have numbers stored in files in a unpredictable format. You can use</P>
<PRE> >getvector(N)
</PRE>
<P>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</P>
<PRE> >{v,M}=getvector(N)
</PRE>
<P>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</P>
<PRE> >A=redim(v,[n,m])</PRE>
<P>There is also the utility function</P>
<PRE> >A=getmatrix(n,m,filename)
</PRE>
<P>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</P>
<PRE> >writematrix(A,filename)
</PRE>
<P>If the filename is not empty, the file will be created (<STRONG>beware!</STRONG>) and closed. Otherwise, you
need to open and close the file yourself.</P>
<H2 ALIGN="CENTER"><A NAME="Primitive Input and Output"></A>Primitive Input and Output</H2>
<P>There are some primitive file input and output routines. The normal method is via dump. However,</P>
<PRE> >open("test.dat","w")
</PRE>
<P>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</P>
<PRE> >open("test.dat","r")
</PRE>
<P>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.</P>
<PRE> >close()
</PRE>
<P>will close the file again.</P>
<PRE> >putchar(c)
</PRE>
<P>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.</P>
<PRE> >putword(x)
>putlongword(x)</PRE>
<P>Puts a word or a vector of words (long words) to the file in binary format.</P>
<PRE> >c=getchar()
</PRE>
<P>reads a character.</P>
<PRE> >v=getchar(n)
</PRE>
<P>reads a vector of n characters.</P>
<PRE> >x=getword()
>x=getlongword()
</PRE>
<P>reads a word or long word.</P>
<PRE> >x=getword(n)
>x=getlongword(n)
</PRE>
<P>reads n words (long words) from a binary file.</P>
<PRE> >s=getstring(n)
</PRE>
<P>reads a string of length n from a binary file.</P>
<P>You can check for the end of the file with</P>
<PRE> >eof()
</PRE>
<P>It will return 1, if the file is completely read.</P>
<PRE> >write("string")
</PRE>
<P>is a function, which writes a string to a text file. You can add a newline with</P>
<PRE> >write("string"|asc(10))</PRE>
<P>
<H2 ALIGN="CENTER"><A NAME="Directories"></A>Directories</H2>
<P>The active directory can be changed with</P>
<PRE> >cd "path"
</PRE>
<P>where path is the new directory and may include a drive letter, like</P>
<PRE> >cd "a:\progs"
</PRE>
<P>The changedir function does the same. It will return the active directory when it is called with "".</P>
<P>The command</P>
<PRE> >dir "*.e"
</PRE>
<P>displays all files in the active directory, which fit with the pattern. An empty string fits all files.</P>
<P>The function</P>
<PRE> >searchfile("*.e");
</PRE>
<P>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.
</BODY>
</HTML>
|