- → 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