Examples/Files: Difference between revisions

From NARS2000
Jump to navigationJump to search
(Examples - Working with Files)
 
m (FileSize more global in scope, ⎕ELX resets to entrant value)
 
Line 7: Line 7:
!style="background:lightgrey; padding:12px;" align="left"|
!style="background:lightgrey; padding:12px;" align="left"|


lNumBytes←FileSize strFileName;iTieNum;iRet <br>
lNumBytes←FileSize strFileName;iTieNum;iRet;strELXwas <br>
⍝Returns file size in bytes.  If file does not exist, returns -1 <br>
⍝Returns file size in bytes.  If file does not exist, returns -1 <br>
⍝Sample call: FileSize "C:/FileName.txt" <br>
⍝Sample call: FileSize "C:/FileName.txt" <br>
strELXwas←⎕ELX <br>
⎕ELX←"→ErrFileSiz"  ⍝On error(e.g. file not found), goto ErrFileSiz line <br>
⎕ELX←"→ErrFileSiz"  ⍝On error(e.g. file not found), goto ErrFileSiz line <br>
iTieNum←strFileName ⎕NTIE 0  ⍝Open file as if for reading, obtain tie number <br>
iTieNum←strFileName ⎕NTIE 0  ⍝Open file as if for reading, obtain tie number <br>
lNumBytes←⎕NSIZE iTieNum  ⍝Read in file size, normal operation, file found <br>
lNumBytes←⎕NSIZE iTieNum  ⍝Read in file size, normal operation, file found <br>
iRet←⎕NUNTIE iTieNum  ⍝Finished with file read, close/untie file # <br>
iRet←⎕NUNTIE iTieNum  ⍝Finished with file read, close/untie file # <br>
⎕ELX←"⎕DM" ⍝Reset error execute function - to its default value <br>
⎕ELX←strELXwas ⍝Reset error execute function - to its entrant value. Note default initial value: ⎕ELX←"⎕DM" <br>
→0 <br>
→0 <br>
ErrFileSiz: lNumBytes←-1  ⍝On error, program branches here; sets # bytes found to -1 <br>
ErrFileSiz: lNumBytes←-1  ⍝On error, program branches here; sets # bytes found to -1 <br>
⎕ELX←"⎕DM" ⍝Reset error execute function - to its default value <br>
⎕ELX←strELXwas ⍝Reset error execute function - to its entrant value. Note default initial value: ⎕ELX←"⎕DM" <br>
→0
→0 <br>
|-
|-
|}
|}
 
APL concepts demonstrated: '''Branching, Commenting, Exiting a Function, System Functions, use of Local Variables, Error Trapping'''.
<hr align="center" color="gray" size="2" width="80%">
<hr align="center" color="gray" size="2" width="80%">
<br>
<br>
See Also: [[System_Function_NNAMES#.E2.8E.95NNAMES|System Functions - Quad NNames]], [[System_Variable_ELX|Quad ELX]]
See Also: [[System_Function_NNAMES#.E2.8E.95NNAMES|System Functions - Quad NNames]], [[System_Variable_ELX|Quad ELX]]

Latest revision as of 11:54, 26 January 2015

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