Difference between revisions of "Read and write text files"
Jhernandez3 (talk | contribs) (Created page with "Category:Analytica User Guide <breadcrumbs>Analytica User Guide > {{PAGENAME}}</breadcrumbs><br /> ==ReadTextFile(filename)== Reads a file filename and returns its conte...") |
Jhernandez3 (talk | contribs) m |
||
Line 4: | Line 4: | ||
==ReadTextFile(filename)== | ==ReadTextFile(filename)== | ||
− | 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: | + | Reads a file <code>filename</code> and returns its contents as a text value. If <code>filename</code> 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: |
:<code>Function LinesFromFile(filename: Atom Text)</code> | :<code>Function LinesFromFile(filename: Atom Text)</code> | ||
Line 12: | Line 12: | ||
:<code>Array(lines, r)</code> | :<code>Array(lines, r)</code> | ||
− | 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. | + | 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. | + | The optional parameter <code>showDialog</code> controls whether the file dialog appears. If not specified, then the dialog appears only if the file does not exist. If you set <code>showDialog</code> 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(filename, text, ''append, warn, sep'')== | ||
− | 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. | + | Writes text to the file <code>filename</code>. The <code>filename</code> 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 <code>False</code> and '''warn''' flag is <code>True</code>. 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 <code>True (1)</code>, and the file already exists, it appends the text to the end of the file. If warn is <code>False (0)</code>, it does not issue a warning before overwriting an existing file when append is <code>False</code>, or when modifying an existing file when append is <code>True</code>. |
− | 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. | + | If text is an array, it writes each element to the file, inserting separator <code>sep</code> between elements, if provided. If text has more than one dimension, you can control the sequence in which they are written by using function [http://wiki.analytica.com/index.php?title=JoinText 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. | + | You can write or append to multiple files when <code>filename</code> 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. |
<footer>Database functions / {{PAGENAME}} / Read data from URL on internet</footer> | <footer>Database functions / {{PAGENAME}} / Read data from URL on internet</footer> |
Revision as of 05:35, 26 December 2015
ReadTextFile(filename)
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:
VAR r := SplitText(ReadTextFile(filename), Chr(10));
Index 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)
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.
Enable comment auto-refresher