Current Page: NeXtMidas User's Guide Files Access from Macros
| #### Accessing Files from Macros NeXtMidas provides two ways of accessing files. The first is using the `FILE` command to open a file which provides a handle to the file that can be used for multiple file operations before closing the file. The other is using the inline `FILE(..)` operator to get a quick handle to the file that can be used to access the file. The downside to using the inline `FILE(..)` operator is that it does a full file-open/file-close for each file operation, whereas the `FILE` command allows the file to be opened once and then accessed many times before closing.
Unlike X-Midas, NeXtMidas does not allow direct indexing into a file with a `fileName(index)` or `fileName(start:end)` syntax. This prevents possible confusion between file operations and operations on a file name.
##### Using the FILE Command Files can be opened using the `FILE` command's `OPEN` function (see the explain file for `FILE` for more details):
    nM> file open/d tag testxy3000
When `FILE` opens a file it returns a handle to the open file (in the above example the handle is named `tag`). Once the file has been opened it can be accessed using the normal `DataFile` methods. For example:
Read the second `Data` element: Do not use this with Type 3000/5000 files. | `nM> res tag.getData(2)`
Read the second record as a `Table`: Works with all file types. | `nM> res tag.getDataTable(2)`
Set the values of the second element: | `nM> invoke ,, tag.setData(2,myTable)`
Insert a new element at position two in the file: | `nM> invoke ,, tag.insertData(2,myTable)`
Add a new element to the end of the file: | `nM> invoke ,, tag.insertData(-1,myTable)`
When done with the file it can be closed using:
    nM> file close tag
Note that when writing files (especially when appending to them) the file will not be automatically flushed to disk unless opened with the `FLUSH` flag (see example in the `FILE` explain file). To manually flush the file to disk use:
    nM> invoke ,, tag.flush()
##### Using the Inline FILE(..) Operator The `FILE(..)` operator takes in the name of a file and converts it into a handle to the file that can be used on the command line. This is useful when doing a few occasional file operations but is inefficient for cases where multiple file operations will occur. To get a record out of a Type 3000 file the `FILE(..)` operator can be used as follows:
    nM> res FILE(testxy3000).getDataTable(3)