ReadTextFile


(Requires Analytica Enterprise, Optimizer, or Power Player)

ReadTextFile(filename, showDialog, title)

Returns the contents a text file «filename» as a text value. «filename» may be the name of a file in the Current Data Directory (initially, the directory containing the model), or a path relative to the Current Data Directory , or an absolute pathname.

If it can't find a file with that name, by default, it opens a file browser dialog to let the user select another file. Set optional parameter, «showDialog» to True (1) to show the file browser dialog, using «filename» as default, even if the file exists. Set «showDialog» to False (0) to never show it. If it can't find the file, it shows an error message.

You can specify the caption for the title bar of the file browser dialog titlebar in the optional «title» parameter.

ADE never shows the file browser dialog, no matter what the parameter values.

Treating End-of-line

There are three common standards in text-files to identify newlines:

  • Windows files usually use the two-character sequence CRLF -- Chr(10) Chr(13)
  • Unix files use a single LF character -- Chr(10)
  • Macintosh OS files use a single CR character -- Chr(13)

Occasional Windows-based programs use one of the other standards.

ReadTextFile insulates you from worrying about which standard is used by converting each to the Unix standard, a single LF -- Chr(10).

If a file contains inconsistent line breaks -- e.g. including more than one examples of LF, CRLF, and CR -- it converts LF and CRLF to a single LF, and leaves CR -- Chr(13) -- as is.

Getting the file name actually opened

Your code may want to know the file path for which file was actually opened. This may differ from «filename» when the specified file is not found, or when «showDialog» forces a dialog, allowing the user to select a different file. ReadTextFile returns the file path as a second return value, which you can optionally capture using, e.g.,

Local (contents, filepath) := ReadTextFile( ... );

A common pattern is that you may want to save the filename in a variable such that when the evaluation is repeated in the future, it can supply the file selected by the user to the «filename» parameter. This can be coded by supplying first a global variable to hold the filename defined using ComputedBy with the default filename as follows:

Variable TheFilename ::=
ComputedBy( TheFileContetns, "defaultFilename" )
Variable TheFileContents ::=
( , TheFilename ) := ReadTextFile( TheFilename )

The assignment to ( , TheFilename ) passes through the first parameter as the result of the assignment expression, but assigns the second return value the TheFilename. The assignment to TheFilename is a side-effect that is allowed only because TheFilename is defined as a ComputedBy. The assignment changes the value, but also rewrites the second parameter of the call to ComputedBy, thus permanently preserving the filename selected. The one line definition of TheFileContents is locally equivalent to:

Local (contents, filename ) := ReadTextFile( TheFilename );
TheFilename := filename;
contents

From ADE

When evaluated in the Analytica Decision Engine (ADE) and a dialog needs to be shown to the end-user, it calls IAdeUICallbacks::GetFilename(...). From within that callback, the parent application can interact with the end-user to resolve the file path, and a web applications can instruct the end-user to upload a file. Once complete, the callback returns the full path to the file which is then read. To receive this callback, the parent application must have previously registered the callback with ADE using CAEngine::SetCallbackObject( ). If it has not registered a callback and the file doesn't exist, returns an empty text.

Once the file read completes, it calls IAdeUICallbacks::FileOpenCompleted().

History

  • The callback in ADE was introduced in ADE 4.6.

See Also

Comments


You are not allowed to post comments.