Thursday, April 3, 2008

GTM write to text file

This post is about creating a text file using a mumps routine and writing some characters to it.
There are 3 main commands that you need to know about.


  1. Open - This opens the file
  2. Use - this tells the mumps environment that write commands should be directed to the given file.
  3. Close - When you have finished close the file.



Set dev="/home/user/testtextfile.txt"


For clarity and usability I like to put the file name in a variable like 'dev'.
If a file is still open when some bad logic in a routine causes it to bomb. Its useful to be able to write 'Close dev' at the command prompt. Before restarting.




Open dev::2

The Open syntax is: the word 'Open' followed by a space, then device name or variable containing the device name. Don't worry about the two colons at the moment. They only serve here to delimit that number 2 at the end. The 2 signifies that the mumps environment should wait up to 2 seconds trying to open a file with the given filename.




If '$Test Write !,"Unable to open file ",dev Quit


When a file is opened sucessfully the system variable $Test is set to a value of 1.
If the file fails to open then $Test is set to 0.
The above line tests for 'not $Test' - so if it can't open the file a useful output will be written to the terminal an the routine / sub-routine exits.



Use dev
W !,"This is some text written to a file"
W !,"This is some more text"


After opening a device you instruct the mumps environment to use it before writing to the file.
Writing to the file is just like writing output to the terminal.
When you have finished close the device.




Close dev


Here is the full routine listing:




Set dev="/home/user/testtextfile.txt"
Open dev::2
If '$Test Write !,"Unable to open file ",dev Quit
Use dev
W !,"This is some text written to a file"
W !,"This is some more text"
Close dev

No comments: