Written by

Retired
Question Brian Downing · Nov 15, 2017

syntax for open/use/read statements on a simple ms-dos text file.

Can anyone suggest suitable Cache ObjectScript syntax for open/use/read statements on a simple ms-dos text file.
I've tried various examples seen on the web but they appear to be implementation specific.
I used MSM MUMPS many years ago but am now retired & would like to write a few simple routines for my own amusement & mental challenge.
I am using the free Version of Cache (V 2017.1) on my Windows 10 PC. The installation file was CachePCkit_x64.exe
My apologies for this query being very basic.

Comments

Robert Cemper · Nov 15, 2017

Welcome back!
infile  ; simple file read
  set filename="C\mydir\myfile.txt"
  set $ZTRAP="end"
  open filename:("R"):0 else  Write "file error"
  for line=1:1 use filename read text use 0 write text,! 
end
 close filename
  set $ZTRAP=""
  use 0 write "Done",
  quit

it's not so sophisticated and I used the end-of-file error to exit

This is also available in class %Library.File with lot more comfort 
http://docs.intersystems.com/latest/csp/documatic/%25CSP.Documatic.cls?…

HTH
 

0
Brian Downing  Nov 15, 2017 to Robert Cemper

Thanks Robert - that works fine on the first run.
But, I'm getting a <ENDOFFILE> error on subsequent runs. 
I'm sure there's a straight forward answer.  Can you help ?

0
Robert Cemper  Nov 15, 2017 to Brian Downing

You have to set $ZTRAP again at the begin:
Or start with do infile^myroutine ....
at the 1. run it is set i line 3
you my also pass the filename as parameter 

0
Alexander Koblov  Nov 16, 2017 to Brian Downing

You might use $ZEOF to check for file end.

It should be enabled first:

do $system.Process.SetZEOF(1)

Then you can read file line-by-line as follows:

  do $system.Process.SetZEOF(1)
  set filename = "c:\temp\qq.txt"
  open filename:"R":2
  if '$Test {
  	write "cannot open file ", filename, !
  	quit
  }
  for  {
  	use filename read str
  	quit:$ZEOF=-1
  	use $Principal write str,!
  }
  close filename
0