<?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, ...)"><<</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" << 2^100 << endl << 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, ...)"><<</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" << "" o3 = testfile o3 : File</pre> </td></tr> <tr><td><pre>i4 : f << "hi" << endl o4 = testfile o4 : File</pre> </td></tr> <tr><td><pre>i5 : f << "ho" << endl o5 = testfile o5 : File</pre> </td></tr> <tr><td><pre>i6 : f << 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>