next | previous | forward | backward | up | top | index | toc | Macaulay2 web site
Macaulay2Doc > The Macaulay2 language > creating and writing files

creating and writing files

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 <<, and we close the file with close. Files must be closed before they can be used for something else.
i1 : "testfile" << 2^100 << endl << close

o1 = testfile

o1 : File
i2 : value get "testfile"

o2 = 1267650600228229401496703205376
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 << to a variable, so we can use it for subsequent print operations and for closing the file.
i3 : f = "testfile" << ""

o3 = testfile

o3 : File
i4 : f << "hi" << endl

o4 = testfile

o4 : File
i5 : f << "ho" << endl

o5 = testfile

o5 : File
i6 : f << close

o6 = testfile

o6 : File
i7 : get "testfile"

o7 = hi
     ho
i8 : removeFile "testfile"