Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 7ebd25ac536d248d499a3ce2acda963a > files > 4186

Macaulay2-1.3.1-8.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8" ?>  <!-- for emacs: -*- coding: utf-8 -*- -->
<!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"	 "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>creating and writing files</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_saving_sppolynomials_spand_spmatrices_spin_spfiles.html">next</a> | <a href="_getting_spinput_spfrom_spthe_spuser.html">previous</a> | <a href="_saving_sppolynomials_spand_spmatrices_spin_spfiles.html">forward</a> | <a href="_getting_spinput_spfrom_spthe_spuser.html">backward</a> | <a href="___The_sp__Macaulay2_splanguage.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div>

    </td>
  </tr>
</table>
<div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_creating_spand_spwriting_spfiles.html" title="">creating and writing files</a></div>
<hr/>
<div><h1>creating and writing files</h1>
<div>We can print to a file in essentially the same way we print to the screen.  In the simplest case, we create the entire file with one command; we give the file name as the initial left hand operand of <a href="__lt_lt.html" title="a binary operator (file output, ...)">&lt;&lt;</a>, and we close the file with <a href="_close.html" title="close a file">close</a>.  Files must be closed before they can be used for something else.<table class="examples"><tr><td><pre>i1 : "testfile" &lt;&lt; 2^100 &lt;&lt; endl &lt;&lt; close

o1 = testfile

o1 : File</pre>
</td></tr>
<tr><td><pre>i2 : value get "testfile"

o2 = 1267650600228229401496703205376</pre>
</td></tr>
</table>
More complicated files may require printing to the file multiple times.  One way to handle this is to assign the open file created the first time we use <a href="__lt_lt.html" title="a binary operator (file output, ...)">&lt;&lt;</a> to a variable, so we can use it for subsequent print operations and for closing the file.<table class="examples"><tr><td><pre>i3 : f = "testfile" &lt;&lt; ""

o3 = testfile

o3 : File</pre>
</td></tr>
<tr><td><pre>i4 : f &lt;&lt; "hi" &lt;&lt; endl

o4 = testfile

o4 : File</pre>
</td></tr>
<tr><td><pre>i5 : f &lt;&lt; "ho" &lt;&lt; endl

o5 = testfile

o5 : File</pre>
</td></tr>
<tr><td><pre>i6 : f &lt;&lt; close

o6 = testfile

o6 : File</pre>
</td></tr>
<tr><td><pre>i7 : get "testfile"

o7 = hi
     ho</pre>
</td></tr>
<tr><td><pre>i8 : removeFile "testfile"</pre>
</td></tr>
</table>
</div>
</div>
</body>
</html>