Read and write text files
ReadTextFile(filename)
This feature is available in Analytica Enterprise and Optimizer.
ReadTextFile reads a file «filename» and returns its contents as a text value. If «filename» contains no directory path, it tries to read from the current folder, usually the folder containing the current model file. If it doesn’t find the file, it opens a Windows browser dialog to prompt the user. For example:
Function LinesFromFile(filename: Atom Text)
Definition:
Local r := SplitText(ReadTextFile(filename), Chr(10));
LocalIndex lines := 1..Size(r);
Array(lines, r)
This function reads in the file and splits the text up at the end of each line, with the Chr(10) line feed character. It then defines a local index lines
, to be used as the index of the array of lines that it returns.
The optional parameter «showDialog» controls whether the file dialog appears. If not specified, then the dialog appears only if the file does not exist. If you set «showDialog» to true (1), it always prompts for the file, even if it finds one by that name. This gives the user a chance to change the filename, while still providing a default name.
WriteTextFile(filename, text, append, warn, sep)
WriteTextFile writes text to the file «filename». The «filename» is relative to the current data directory. It returns the full pathname of the file if it is successful in writing or appending to it. By default, the «append» flag is False
and «warn» flag is True
. If the file doesn’t already exist, it creates the file in the current data directory — and if the file does exist, it asks if you want to replace it. If «append» is True (1)
, and the file already exists, it appends the text to the end of the file. If warn is False (0)
, it does not issue a warning before overwriting an existing file when append is False
, or when modifying an existing file when append is True
.
If text is an array, it writes each element to the file, inserting separator «sep» between elements, if provided. If text has more than one dimension, you can control the sequence in which they are written by using function JoinText to join the text over the index you want innermost.
You can write or append to multiple files when «filename» is an array of file names. If text has the same index(es), it writes the corresponding slice of text to each file — following proper array abstraction.
See Also
- ReadTextFile
- WriteTextFile
- Excel spreadsheets read and write
- Read data from URL on internet
- ReadBinaryFile
- Files and Editing
Enable comment auto-refresher