(***********************************************************************
Mathematica-Compatible Notebook
This notebook can be used on any computer system with Mathematica 3.0,
MathReader 3.0, or any compatible application. The data for the notebook
starts with the line of stars above.
To get the notebook into a Mathematica-compatible application, do one of
the following:
* Save the data starting with the line of stars above into a file
with a name ending in .nb, then open the file inside the application;
* Copy the data starting with the line of stars above to the
clipboard, then use the Paste menu command inside the application.
Data for notebooks contains only printable 7-bit ASCII and can be
sent directly in email or through ftp in text mode. Newlines can be
CR, LF or CRLF (Unix, Macintosh or MS-DOS style).
NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing the
word CacheID, otherwise Mathematica-compatible applications may try to
use invalid cache data.
For more information on notebooks and Mathematica-compatible
applications, contact Wolfram Research:
web: http://www.wolfram.com
email: info@wolfram.com
phone: +1-217-398-0700 (U.S.)
Notebook reader applications are available free of charge from
Wolfram Research.
***********************************************************************)
(*CacheID: 232*)
(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[ 51661, 1756]*)
(*NotebookOutlinePosition[ 52746, 1791]*)
(* CellTagsIndexPosition[ 52702, 1787]*)
(*WindowFrame->Normal*)
Notebook[{
Cell[CellGroupData[{
Cell[TextData[" 25. Files and Representations of Data"], "Title",
Evaluatable->False,
AspectRatioFixed->True],
Cell["Last revision: April 15 1998", "SmallText",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
"In this section we consider files, directories, and ",
StyleBox["Mathematica",
FontSlant->"Italic"],
"'s operations with them."
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" operations/structures introduced: \n",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox[" ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["files",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[
"\n Directory[]\n $Path\n FileNames[]\n FileDate[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["file",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["]\n FileByteCount[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["file",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["]\n RenameFile[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["file1",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[", ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["file2",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["]\n CopyFile[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["file1",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[", ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["file2",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["]\n DeleteFile[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["file",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[
"]\n SetDirectory[]\n OpenRead[]\n OpenWrite[]\n \
OpendAppend[]\n InputStream[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["filename",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[", ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["number",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["]\n OutputStream[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["filename",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[", ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["number",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["]\n Close[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["fileref",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["]\n Write[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["fileref",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[", ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["data",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["]\n WriteString[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["fileref",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[", ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["data",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["]\n !!",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["filename",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["\n Read[]\n ReadList[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"]
}], "SmallText",
Evaluatable->False,
AspectRatioFixed->True],
Cell[CellGroupData[{
Cell[TextData["Files"], "Section",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Files",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" are collections of data on a computer, typically residing on a permanent \
storage device such as a hard drive or a floppy disk. Files can contain data \
of any data type, in any order; in this respect, they are completely general. \
Their very generality, however, together with the privileges programmers \
enjoy in modifying them, leads us to issue a caveat or two at the outset. \
First, programs that read and write files usually write data in a certain \
form and, when reading data, expect to find it in a similar form; hence \
programmers must be careful when dealing with files, lest some data be read \
in which is completely meaningless but which will have bad consequences \
during a program's execution. Similarly, programs have the power to rename, \
copy, and delete files, and programmers should write programs which do so \
only under well-defined circumstances, in order to prevent costly mistakes. \
In what follows we will first insure that we know where our files will \
reside, then discuss how to create and alter their contents, and finally show \
an example of good programming practice---checking to see whether a given \
file is one with which we wish to work.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[CellGroupData[{
Cell[TextData["Directories and File Characteristics"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Below we will treat the standard file operations, those of opening, \
closing, reading, and writing, but first we must make sure we know where the \
files we create will be located on our computer. Files on computers are \
usually organized into ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["directories",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[", or ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["folders",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
", which are collections of files and directories. In many respects \
directories are like contexts: directories can contain (sub)directories just \
as contexts can contain (sub)contexts; directories hold files while contexts \
hold variables and functions; there is a default directory just like there is \
a default context; and there is a directory search path used in the same \
manner as a context search path. First, let's find our current default \
directory, using \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Directory[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["Directory[]"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Now let's find out our directory search path, \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["$Path",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["$Path"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
"If we ask ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" to read a file with a certain name, it will first look in the default \
directory, and, if it does not find the file there, it will proceed to the \
directories in \"$Path\", checking them one at a time. Note that the default \
directory and the elements of \"$Path\" are all strings. "
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[CellGroupData[{
Cell[TextData["FileNames[]"], "Subsubsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["FileNames[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" instructs ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" to list the files in the default directory.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["FileNames[]"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"If we don't want a complete list, but want all file names which contain \
certain characters, we can use ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["wild-card",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" expressions. Wild-card expressions are to some extent system-dependent, \
but usually an expression with an asterisk (\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["*",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\") in one or more places is a legal wild-card expression, denoting that \
in a file name, any characters may appear in place of the asterisk. For \
instance, \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["A*",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" would denote all file names beginning with \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["A",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\". Let's see if we have any such names in our default directory.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["FileNames[\"A*\"]"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["FileNames[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" will even allow a list of wild-card expressions; we could ask for all \
file names beginning with \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["A",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" or \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["B",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" by entering",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["FileNames[{\"A*\",\"B*\"}]"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Even more, however, we can have \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["FileNames[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" list files in another directory, without switching default directories, \
simply by referring to files with their long forms (analogously to variable \
names' long forms). On an IBM computer, then, we could list files at the top \
directory of the \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["C:",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" drive by executing",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["FileNames[{\"C:*\",\"C:*.*\"}]"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"(This last list was to insure that we would get all files, whether they \
contained an extension or not.)"], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData[
"Information on files; copying, renaming, and deleting"], "Subsubsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" has many commands for determining information associated to files, as \
well as for changing that information. Two of the more frequently used for \
determining information are \n\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["FileDate[file]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", which gives the last-modified date of the file in the list form \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["{year, month, day, hour, minute, second}",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["FileByteCount[file]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", which returns then number of bytes in the file, a measure of its size. \
Several of the more frequently used for altering information are \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["CopyFile[file1, file2]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", which copies file \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["file1",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" to \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["file2",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\"; \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["RenameFile[file1,file2]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" which changes the name of \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["file1",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" to \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["file2",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\"; and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["DeleteFile[file]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", which deletes file \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["file",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\". Note that in the preceding commands, \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["file",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["file1",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["file2",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" all must be strings.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Changing the default directory: Important!"], "Subsubsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["To change the default directory, use the \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["SetDirectory[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" command. Ask your instructor what a good place on your hard drive (or \
floppy disk) would be to store and read temporary files, and set your default \
directory to that directory.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Opening, Reading or Writing, and Closing"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[CellGroupData[{
Cell[TextData["Opening and Closing; Stream Expressions"], "Subsubsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Before opening a file, one must decide whether the file will be opened to \
",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["read",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" data, ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["write",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" data, or ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["append",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" data. Some computer systems allow us to do some combination of these to \
the same file, but we will consider only these three primitive functions. If \
we open a file for reading, we will not be able to write to it (unless we \
close it and then open it back up); if we open a file for writing, we will \
erase any file that was present with the same name, create a new file of the \
same name, and will not be able to read from it (unless we close it and open \
it back up); and if we open a file for appending, we will add data on to the \
end of a previously-existing file and will similarly be prevented from \
reading data. Note that it is important to distinguish between opening a \
file for writing and opening a file for appending.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["The three open commands are as follows: \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["OpenRead[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["OpenWrite[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["OpenAppend[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\". The arguments, as usual, are strings representing the file names. \
After performing their functions, the three commands return an expression of \
the form \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["InputStream[filename,number]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" (for reading) or \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["OutputStream[filename,number]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\". In each case, ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" assigns each open file a unique number to distinguish it from other open \
files. When we wish to refer to our open file later on, we will need to \
refer to it via its stream expression \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["InputStream",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["...\" or \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["OutputStream",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"...\"; hence we will usually assign this long expression to a variable. ",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["Let's open a file for writing, calling it \"F1\". "], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["fileref = OpenWrite[\"F1\"]"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Now, to close a file, we use the \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Close[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" command, giving it as its single argument the stream expression. In \
our case above, we've stored the expression in \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["fileref",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", so we can execute \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Close[fileref]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" to close the file.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["Close[fileref]"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Note: if you forget to assign the stream expression to a variable, you \
cannot rectify the situation by opening a file again; ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" will not allow a file to be opened twice. Hence you must close it. What \
if you've forgotten the stream expression, knowing the file name but not the \
number? Use \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Streams[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", which provides a list of all open files (and other streams). ",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"Now let's see if a file by the name of \"F1\" was really created."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["FileNames[]"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"We have verified that \"F1\" exists, and now we'd like to verify that it has \
nothing inside. We can do so by checking its size."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["FileByteCount[\"F1\"]"], "Input",
AspectRatioFixed->True],
Cell[TextData["Finally, let's delete this file."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["DeleteFile[\"F1\"]"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"It is important to remember always to close a file after you are finished \
reading or writing it. The operation of closing allows the computer system \
to \"tidy up\" details related to the file, and thus the chances of having \
your data damaged in the file are decreased."], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Reading and Writing"], "Subsubsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Before getting started, we should agree on some terminology. ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Writing to",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" a file means that the program will send data ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["to",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" the file, which will then be stored on a floppy disk or hard drive. ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Reading from",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" a file means that the program will ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["receive",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" data from the file, which will then be stored in variables.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"We begin by opening a file to which we will write and from which we will \
then read."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["fileref = OpenWrite[\"FILE1\"];"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["To write to a file, we use the command \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Write[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["file",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[", ... ]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" or \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["WriteString[",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["file",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[", ... ]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\". In each case, the first argument is the stream expression, typically \
contained in a variable such as \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["fileref",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\". The succeeding arguments, separated by commas as usual, are the \
expressions, or names of expressions, that we wish ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" to write out. The differences between \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Write[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["WriteString[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" are twofold and important: the former writes out strings with quotation \
marks, and then ensures that any further data will be written on a new line \
in the file, while the latter writes out strings without quotation marks and \
allows further data to be written on the same line. We'll perform examples \
of both, and check this behavior when we read the file back in below.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"First, we'll write the number 1, the String \"Hello\", and then the result \
of the expression \"Apply[Plus,{1,2,3}]\", making sure that any further \
output will begin on a new line."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["Write[fileref,1,\"Hello\",Apply[Plus,{1,2,3}]];"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Without formally opening a file, there is a quick way to ask ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" what the entire contents of the file are: \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["!!",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\". Execute \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["!!",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["file",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["\", where \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["file",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["\" is the file name (without quotation marks!), and ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" will simply dump the contents of the file onto the screen.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["!!FILE1"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"We notice that when we write to a file, there are no default spaces between \
the characters; if we want spaces, we'll have to write them out separately."],
"Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"WriteString[fileref,\"The answers are \"];\nWriteString[fileref,1];\n\
WriteString[fileref,\" and \"];\nWrite[fileref,2];"], "Input",
AspectRatioFixed->True],
Cell[TextData["!!FILE1"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
"Notice how important it was that the first three commands were \
\"WriteString[]\", and the last \"Write[]\"; in the first cases we wished to \
insure that the strings were written without quotation marks and the data \
remained on the same line, while at the end we wanted to make sure ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" moved to a new line after writing \"2\"."
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"Now let's close the file and read its contents back in."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["Close[fileref]"], "Input",
AspectRatioFixed->True],
Cell[TextData["fileref = OpenRead[\"FILE1\"]"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"In reading a file, we must decide how we want to read the text of the file \
back in. For instance, if the file begins \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["12",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", is this a number, or part of a string beginning \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["12Hello",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\"? ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" needs to be told what sort of data to expect from the file. We are \
allowed to specify several ways to convert a sequence of characters into a \
values of given ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" data types, as follows.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Character",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" instructs ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" to read in a single character---even a blank---and put it in its own \
String.\n\n\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Number",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" instructs ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" to read several numeric characters, including decimal points, until it \
finds a blank or other non-numeric character, and then convert the string of \
characters into a number. The number will be exact if no decimal point is \
encountered, approximate otherwise.\n\n\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Word",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" instructs ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" to read in characters until a \"word separator\"---typically a blank---is \
encountered and then to convert the sequence of characters into a String. \
(The standard word separators are \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox[" ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" (blank) and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["\\t",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" (tab); these can be changed as an option in \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["ReadList[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" or \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Read[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\"; for more information, consult the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" manual.)\n\n\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["String",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" instructs ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" to read in characters until it reaches the end of a line in the file and \
then to convert the characters into one String.\n\n\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Byte",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" instructs ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" to read in a single byte from the file and to return its numeric value as \
an Integer. Usually, the byte will be a single character, converted to its \
character code.\n\n\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Expression",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" instructs ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" to read in a complete ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" expression.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Once we've decided what to read in, we have to functions we can use to \
perform the read operation, \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Read[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" and \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["ReadList[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\". Each require the the first argument be the stream expression and that \
the second argument be one of the conversion types listed above. The \
difference is that \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Read[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" reads in a single value of the conversion type, while \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["ReadList[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" attempts to read in several instances of the type, placing them into a \
list. Without a third argument, \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["ReadList[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" will attempt to read in the entire file as values of the specified \
type; with a third argument \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["n",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["ReadList[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" will stop after reading in \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["n",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" instances of the type.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"First, let's read in a single Number, which we expect to be \"1\"."], "Text",\
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["num = Read[fileref,Number]"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Now let's read in up to three characters, which we expect to be a \
quotation mark, an \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["H",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", and an \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["e",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["charlist = ReadList[fileref,Character,3]"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["We'll now read in the rest of the line as a ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["String",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["restOfLine = Read[fileref,String]"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Our \"place\" in the file, which means the point at which ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" is keeping its finger in order to decide what data to read in next, is \
now at the beginning of the second line. Let's read in the line, separated \
into words. We'll use \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["ReadList[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" with no third argument because we're intending to continue reading \
words until we reach the end of the file.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["finalWords = ReadList[fileref,Word]"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Note that the fourth and sixth elements of the preceding list are ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["String",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["s, not numbers.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["Now let's close the file."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["Close[fileref]"], "Input",
AspectRatioFixed->True]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Checking That We've Got the Right File"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"One good programming habit, with regard to files, is always to write out \
some special string in the front of a file that your program may need to read \
back in. In this way we can check whether or not a file, the name of which \
has been provided by the user, is in fact a file of the correct type for our \
program. For instance, in our package \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Sports`",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", we may wish to save the roster to a file and read it back in. We \
could store the String ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["\"This file written by package Sports\"",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" in the front of the file and check for the existence of this string when \
we read a file back in. ",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Examples"], "Section",
Evaluatable->False,
AspectRatioFixed->True],
Cell[CellGroupData[{
Cell[TextData["Storing a List of Numbers"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"As our first example we show a method to write out a list of numbers into \
a file for easy access later. We will define a function \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["storeInFile[list,file]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\", where the first argument is the list of numbers and the second \
argument is a ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["String",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" representing a file name. Our function will open the file, store the \
data, and close the file. Notice that the function writes out each element \
of the list individually, instead of writing the entire list all at once; \
this will permit us to read in the elements one at a time, if desired, \
without reading in the entire list.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"Clear[storeInFile]\nstoreInFile[\n\t\t\
list_?((Select[Map[NumberQ,#],False]=={})&),\n\t\ts_String] :=\n\t\t\
Module[{myStream},\n\t\t myStream = OpenWrite[s];\n\t\t Do[\n\t\t \
Write[myStream,list[[i]]],\n\t\t {i,Length[list]}\n\t\t ];\n\t\t \
Close[myStream];\n\t\t];\n\t\t"], "Input",
AspectRatioFixed->True],
Cell[TextData["We test it out with the following code."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["storeInFile[{1,2,3,4,5},\"MYFILE\"]"], "Input",
AspectRatioFixed->True],
Cell[TextData["!!MYFILE"], "Input",
AspectRatioFixed->True]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Reading a List of Numbers"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"For our second example, we define a function to read in a list of numbers \
from a file."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"Clear[readMyFile]\nreadMyFile[s_String] :=\n Module[{myStream,l},\n \
myStream = OpenRead[s];\n l = ReadList[myStream,Number];\n \
Close[myStream];\n l\n ];"], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["We test this function out on the file \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["MYFILE",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" created above. Again, note that if the function above had not written \
out the numbers individually, but instead had written the entire list all at \
once, our function here would get into trouble, attempting to convert \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["{",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" into a number.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["readMyFile[\"MYFILE\"]"], "Input",
AspectRatioFixed->True]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Placing a Type Flag at the Front of the File"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Finally, we'll rewrite the previous two examples, providing a ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["String",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" at the front of the file to give us assurance that the files we're \
reading are ones which we wrote.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"Clear[storeInFile]\nstoreInFile[\n\t\t\
list_?((Select[Map[NumberQ,#],False]=={})&),\n\t\ts_String] :=\n\t\t\
Module[{myStream},\n\t\t myStream = OpenWrite[s];\n\t\t \
WriteString[myStream,\"Specially created!\\n\"];\n\t\t Do[\n\t\t \
Write[myStream,list[[i]]],\n\t\t {i,Length[list]}\n\t\t ];\n\t\t \
Close[myStream];\n\t\t];\n\t\t"], "Input",
AspectRatioFixed->True],
Cell[TextData[
"Clear[readMyFile]\nreadMyFile[s_String] :=\n Module[{myStream,l,ss},\n \
myStream = OpenRead[s];\n ss = Read[myStream,String];\n \
If[ss==\"Specially created!\",\n l = ReadList[myStream,Number],\n \
Print[\"Invalid file!\"]; l={}];\n Close[myStream];\n l\n ];"],
"Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Note that since we were using \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["WriteString[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\", we had to tell ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" explicitly to end the line in writing the special code out (using \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["\\n",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\"), but when we read the string back in, the end-of-line character \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["\\n",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" is not contained in the resulting string (\"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["ss",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" above).",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"On some systems we are not allowed to re-write onto a file that already \
exists (and sometimes the \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["DeleteFile[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\" command does not work); hence we'll switch to \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["MYFILE2",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["storeInFile[{1,2,3,4,5},\"MYFILE2\"]"], "Input",
AspectRatioFixed->True],
Cell[TextData["!!MYFILE2"], "Input",
AspectRatioFixed->True],
Cell[TextData["readMyFile[\"MYFILE2\"]"], "Input",
AspectRatioFixed->True]
}, Closed]]
}, Closed]]
}, Open ]]
},
FrontEndVersion->"Macintosh 3.0",
ScreenRectangle->{{0, 832}, {0, 604}},
WindowToolbars->{},
WindowSize->{566, 562},
WindowMargins->{{124, Automatic}, {Automatic, 1}},
PrintingCopies->1,
PrintingPageRange->{1, Automatic},
PrivateNotebookOptions->{"ColorPalette"->{RGBColor, -1}},
ShowCellLabel->True,
ShowCellTags->False,
RenderingOptions->{"ObjectDithering"->True,
"RasterDithering"->False},
MacintoshSystemPageSetup->"\<\
00<0004/0B`000003809T?o>old"
]
(***********************************************************************
Cached data follows. If you edit this Notebook file directly, not using
Mathematica, you must remove the line containing CacheID at the top of
the file. The cache data will then be recreated when you save this file
from within Mathematica.
***********************************************************************)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[CellGroupData[{
Cell[1731, 51, 113, 2, 136, "Title",
Evaluatable->False],
Cell[1847, 55, 98, 2, 26, "SmallText",
Evaluatable->False],
Cell[1948, 59, 214, 7, 32, "Text",
Evaluatable->False],
Cell[2165, 68, 4726, 179, 320, "SmallText",
Evaluatable->False],
Cell[CellGroupData[{
Cell[6916, 251, 82, 2, 50, "Section",
Evaluatable->False],
Cell[7001, 255, 1435, 26, 142, "Text",
Evaluatable->False],
Cell[CellGroupData[{
Cell[8461, 285, 116, 2, 42, "Subsection",
Evaluatable->False],
Cell[8580, 289, 1394, 38, 90, "Text",
Evaluatable->False],
Cell[9977, 329, 64, 1, 22, "Input"],
Cell[10044, 332, 361, 13, 25, "Text",
Evaluatable->False],
Cell[10408, 347, 58, 1, 22, "Input"],
Cell[10469, 350, 436, 10, 51, "Text",
Evaluatable->False],
Cell[CellGroupData[{
Cell[10930, 364, 94, 2, 33, "Subsubsection",
Evaluatable->False],
Cell[11027, 368, 547, 20, 25, "Text",
Evaluatable->False],
Cell[11577, 390, 64, 1, 22, "Input"],
Cell[11644, 393, 1321, 42, 64, "Text",
Evaluatable->False],
Cell[12968, 437, 70, 1, 22, "Input"],
Cell[13041, 440, 774, 29, 38, "Text",
Evaluatable->False],
Cell[13818, 471, 79, 1, 22, "Input"],
Cell[13900, 474, 793, 24, 51, "Text",
Evaluatable->False],
Cell[14696, 500, 83, 1, 22, "Input"],
Cell[14782, 503, 180, 4, 25, "Text",
Evaluatable->False]
}, Closed]],
Cell[CellGroupData[{
Cell[14999, 512, 137, 3, 33, "Subsubsection",
Evaluatable->False],
Cell[15139, 517, 3312, 114, 116, "Text",
Evaluatable->False]
}, Closed]],
Cell[CellGroupData[{
Cell[18488, 636, 125, 2, 33, "Subsubsection",
Evaluatable->False],
Cell[18616, 640, 549, 16, 38, "Text",
Evaluatable->False]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[19214, 662, 120, 2, 42, "Subsection",
Evaluatable->False],
Cell[CellGroupData[{
Cell[19359, 668, 122, 2, 33, "Subsubsection",
Evaluatable->False],
Cell[19484, 672, 1484, 39, 103, "Text",
Evaluatable->False],
Cell[20971, 713, 2120, 70, 90, "Text",
Evaluatable->False],
Cell[23094, 785, 125, 2, 25, "Text",
Evaluatable->False],
Cell[23222, 789, 80, 1, 22, "Input"],
Cell[23305, 792, 865, 29, 38, "Text",
Evaluatable->False],
Cell[24173, 823, 67, 1, 22, "Input"],
Cell[24243, 826, 860, 26, 64, "Text",
Evaluatable->False],
Cell[25106, 854, 140, 3, 25, "Text",
Evaluatable->False],
Cell[25249, 859, 64, 1, 22, "Input"],
Cell[25316, 862, 205, 4, 25, "Text",
Evaluatable->False],
Cell[25524, 868, 74, 1, 22, "Input"],
Cell[25601, 871, 106, 2, 25, "Text",
Evaluatable->False],
Cell[25710, 875, 71, 1, 22, "Input"],
Cell[25784, 878, 350, 6, 51, "Text",
Evaluatable->False]
}, Closed]],
Cell[CellGroupData[{
Cell[26171, 889, 102, 2, 33, "Subsubsection",
Evaluatable->False],
Cell[26276, 893, 1095, 35, 51, "Text",
Evaluatable->False],
Cell[27374, 930, 160, 4, 25, "Text",
Evaluatable->False],
Cell[27537, 936, 84, 1, 22, "Input"],
Cell[27624, 939, 2348, 75, 103, "Text",
Evaluatable->False],
Cell[29975, 1016, 258, 5, 38, "Text",
Evaluatable->False],
Cell[30236, 1023, 100, 1, 22, "Input"],
Cell[30339, 1026, 1377, 47, 51, "Text",
Evaluatable->False],
Cell[31719, 1075, 60, 1, 22, "Input"],
Cell[31782, 1078, 231, 5, 38, "Text",
Evaluatable->False],
Cell[32016, 1085, 175, 3, 50, "Input"],
Cell[32194, 1090, 60, 1, 22, "Input"],
Cell[32257, 1093, 466, 10, 51, "Text",
Evaluatable->False],
Cell[32726, 1105, 130, 3, 25, "Text",
Evaluatable->False],
Cell[32859, 1110, 67, 1, 22, "Input"],
Cell[32929, 1113, 82, 1, 22, "Input"],
Cell[33014, 1116, 1196, 39, 64, "Text",
Evaluatable->False],
Cell[34213, 1157, 4315, 146, 233, "Text",
Evaluatable->False],
Cell[38531, 1305, 2129, 71, 90, "Text",
Evaluatable->False],
Cell[40663, 1378, 143, 4, 25, "Text",
Evaluatable->False],
Cell[40809, 1384, 79, 1, 22, "Input"],
Cell[40891, 1387, 578, 22, 25, "Text",
Evaluatable->False],
Cell[41472, 1411, 93, 1, 22, "Input"],
Cell[41568, 1414, 356, 13, 25, "Text",
Evaluatable->False],
Cell[41927, 1429, 86, 1, 22, "Input"],
Cell[42016, 1432, 841, 25, 51, "Text",
Evaluatable->False],
Cell[42860, 1459, 88, 1, 22, "Input"],
Cell[42951, 1462, 395, 14, 25, "Text",
Evaluatable->False],
Cell[43349, 1478, 99, 2, 25, "Text",
Evaluatable->False],
Cell[43451, 1482, 67, 1, 22, "Input"]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[43567, 1489, 118, 2, 42, "Subsection",
Evaluatable->False],
Cell[43688, 1493, 1071, 29, 77, "Text",
Evaluatable->False]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[44808, 1528, 85, 2, 30, "Section",
Evaluatable->False],
Cell[CellGroupData[{
Cell[44918, 1534, 105, 2, 42, "Subsection",
Evaluatable->False],
Cell[45026, 1538, 1058, 29, 77, "Text",
Evaluatable->False],
Cell[46087, 1569, 347, 6, 140, "Input"],
Cell[46437, 1577, 113, 2, 25, "Text",
Evaluatable->False],
Cell[46553, 1581, 88, 1, 22, "Input"],
Cell[46644, 1584, 61, 1, 22, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[46742, 1590, 105, 2, 42, "Subsection",
Evaluatable->False],
Cell[46850, 1594, 163, 4, 25, "Text",
Evaluatable->False],
Cell[47016, 1600, 228, 4, 90, "Input"],
Cell[47247, 1606, 762, 23, 51, "Text",
Evaluatable->False],
Cell[48012, 1631, 75, 1, 22, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[48124, 1637, 124, 2, 42, "Subsection",
Evaluatable->False],
Cell[48251, 1641, 478, 15, 38, "Text",
Evaluatable->False],
Cell[48732, 1658, 406, 7, 150, "Input"],
Cell[49141, 1667, 352, 6, 120, "Input"],
Cell[49496, 1675, 1240, 43, 51, "Text",
Evaluatable->False],
Cell[50739, 1720, 646, 22, 38, "Text",
Evaluatable->False],
Cell[51388, 1744, 89, 1, 22, "Input"],
Cell[51480, 1747, 62, 1, 22, "Input"],
Cell[51545, 1750, 76, 1, 22, "Input"]
}, Closed]]
}, Closed]]
}, Open ]]
}
]
*)
(***********************************************************************
End of Mathematica Notebook file.
***********************************************************************)