This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
 2.11.1 Reading and Writing Mathematica Files Particularly if you use a text-based Mathematica interface, you will often need to read and write files containing definitions and results from Mathematica. Section 1.11.1 gave a general discussion of how to do this. This section gives some more details. Reading files. This shows the contents of the file factors. In[1]:= !!factors (* Factors of x^20 - 1 *) (-1 + x)*(1 + x)*(1 + x^2)*(1 - x + x^2 - x^3 + x^4)* (1 + x + x^2 + x^3 + x^4)*(1 - x^2 + x^4 - x^6 + x^8) This reads in the file, and returns the last expression in it. In[2]:= <> tmp Here are the contents of the file. In[5]:= !!tmp (-1 + x)*(1 + x)*(1 - x + x^2)*(1 + x + x^2) This appends another expression to the same file. In[6]:= Factor[x^8 - 1] >>> tmp Both expressions are now in the file. In[7]:= !!tmp (-1 + x)*(1 + x)*(1 - x + x^2)*(1 + x + x^2)(-1 + x)*(1 + x)*(1 + x^2)*(1 + x^4) When you use expr>>>file, Mathematica appends each new expression you give to the end of your file. If you use expr>>file, however, then Mathematica instead wipes out anything that was in the file before, and then puts expr into the file. When you use either >> or >>> to write expressions to files, the expressions are usually given in Mathematica input format, so that you can read them back into Mathematica. Sometimes, however, you may want to save expressions in other formats. You can do this by explicitly wrapping a format directive such as OutputForm around the expression you write out. This writes an expression to the file tmp in output format. In[8]:= OutputForm[ Factor[x^6 - 1] ] >> tmp The expression in tmp is now in output format. In[9]:= !!tmp 2 2(-1 + x) (1 + x) (1 - x + x ) (1 + x + x ) One of the most common reasons for using files is to save definitions of Mathematica objects, to be able to read them in again in a subsequent Mathematica session. The operators >> and >>> allow you to save Mathematica expressions in files. You can use the function Save to save complete definitions of Mathematica objects, in a form suitable for execution in subsequent Mathematica sessions. Writing out definitions. This assigns a value to the symbol a. In[10]:= a = 2 - x^2 Out[10]= You can use Save to write the definition of a to a file. In[11]:= Save["afile", a] Here is the definition of a that was saved in the file. In[12]:= !!afile a = 2 - x^2 When you define a new object in Mathematica, your definition will often depend on other objects that you defined before. If you are going to be able to reconstruct the definition of your new object in a subsequent Mathematica session, it is important that you store not only its own definition, but also the definitions of other objects on which it depends. The function Save looks through the definitions of the objects you ask it to save, and automatically also saves all definitions of other objects on which it can see that these depend. However, in order to avoid saving a large amount of unnecessary material, Save never includes definitions for symbols that have the attribute Protected. It assumes that the definitions for these symbols are also built in. Nevertheless, with such definitions taken care of, it should always be the case that reading the output generated by Save back into a new Mathematica session will set up the definitions of your objects exactly as you had them before. This defines a function f which depends on the symbol a defined above. In[13]:= f[z_] := a^2 - 2 This saves the complete definition of f in a file. In[14]:= Save["ffile", f] The file contains not only the definition of f itself, but also the definition of the symbol a on which f depends. In[15]:= !!ffile f[z_] := a^2 - 2a = 2 - x^2 The function Save makes use of the output forms Definition and FullDefinition, which print as definitions of Mathematica symbols. In some cases, you may find it convenient to use these output forms directly. The output form Definition[f] prints as the sequence of definitions that have been made for f. In[16]:= Definition[f] Out[16]= FullDefinition[f] includes definitions of the objects on which f depends. In[17]:= FullDefinition[f] Out[17]= When you create files for input to Mathematica, you usually want them to contain only "plain text", which can be read or modified directly. Sometimes, however, you may want the contents of a file to be "encoded" so that they cannot be read or modified directly as plain text, but can be loaded into Mathematica. You can create encoded files using the Mathematica function Encode. Creating and reading encoded files. This writes an expression in plain text to the file tmp. In[18]:= Factor[x^2 - 1] >> tmp This writes an encoded version of the file tmp to the file tmp.x. In[19]:= Encode["tmp", "tmp.x"] Here are the contents of the encoded file. The only recognizable part is the special Mathematica comment at the beginning. In[20]:= !!tmp.x (*!1N!*)mcm_QZ9tcI1cfre*Wo8:) P Even though the file is encoded, you can still read it into Mathematica using the << operator. In[21]:= <