Examples/Files

From NARS2000
Revision as of 11:54, 26 January 2015 by Robert Wallick (talk | contribs) (FileSize more global in scope, ⎕ELX resets to entrant value)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
Examples - Go back to examples

Example 1 - NARS2000 User APL Function: FileSize     Uses System Functions/Variables: ⎕NTIE ⎕NSIZE ⎕NUNTIE ⎕ELX

lNumBytes←FileSize strFileName;iTieNum;iRet;strELXwas
⍝Returns file size in bytes. If file does not exist, returns -1
⍝Sample call: FileSize "C:/FileName.txt"
strELXwas←⎕ELX
⎕ELX←"→ErrFileSiz" ⍝On error(e.g. file not found), goto ErrFileSiz line
iTieNum←strFileName ⎕NTIE 0 ⍝Open file as if for reading, obtain tie number
lNumBytes←⎕NSIZE iTieNum ⍝Read in file size, normal operation, file found
iRet←⎕NUNTIE iTieNum ⍝Finished with file read, close/untie file #
⎕ELX←strELXwas ⍝Reset error execute function - to its entrant value. Note default initial value: ⎕ELX←"⎕DM"
→0
ErrFileSiz: lNumBytes←-1 ⍝On error, program branches here; sets # bytes found to -1
⎕ELX←strELXwas ⍝Reset error execute function - to its entrant value. Note default initial value: ⎕ELX←"⎕DM"
→0

APL concepts demonstrated: Branching, Commenting, Exiting a Function, System Functions, use of Local Variables, Error Trapping.



See Also: System Functions - Quad NNames, Quad ELX