home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 January / CHIPCD1_98.iso / software / tipsy / zoc / install.fil / SCRIPT / RXSAMPLE / TUTORIAL / 8_FILEIO.ZRX < prev    next >
Text File  |  1996-11-19  |  2KB  |  69 lines

  1. /* REXX 
  2. ** 
  3. **  This example demonstrates the basic file functions:
  4. **
  5. **  - check for existence of file
  6. **
  7. **  - open file for writing 
  8. **  - write data to file
  9. **  - close file
  10. **
  11. **  - open file for reading 
  12. **  - read data and check for end of file
  13. **  - close file
  14. **
  15. */
  16.  
  17. /* ------------------------------------------------------------------ */
  18.  
  19. /* the STREAM(,"C","QUERY EXISTS") function call can be used to check */
  20. /* if a file for exists.                                              */
  21.  
  22. IF STREAM("SOME.TXT", "C", "QUERY EXISTS")\="" THEN DO
  23.     /* file exists, so delete it */
  24.     ADDRESS CMD "DEL SOME.TXT"
  25. END
  26.  
  27. /* ------------------------------------------------------------------ */
  28.  
  29. /* the STREAM(,"C","OPEN WRITE") call is used to open a file.         */
  30. CALL STREAM "SOME.TXT", "C", "OPEN WRITE"
  31.  
  32. /* the LINEOUT call writes data to a file.  Instead of a file handle, */
  33. /* the file name is used.                                             */
  34.  
  35. CALL LINEOUT "SOME.TXT", "THIS IS A LINE OF TEXT"
  36. CALL LINEOUT "SOME.TXT", "THIS IS SOME MORE TEXT"
  37. CALL LINEOUT "SOME.TXT", ""
  38. CALL LINEOUT "SOME.TXT", "MORE STUFF AFTER AN EMPTY LINE"
  39.  
  40. /* the STREAM call can be used to close a file.                       */
  41. CALL STREAM "SOME.TXT", "C", "CLOSE"
  42.  
  43. /* ------------------------------------------------------------------ */
  44.  
  45. /* now we open the file again and read the data back in via LINEIN    */
  46. /* function.  End-Of-File is detected via STREAM(,"S") function       */
  47.  
  48. CALL STREAM "SOME.TXT", "C", "OPEN READ"
  49.  
  50. DO FOREVER 
  51.     /* read one line of text */
  52.     line= LINEIN("SOME.TXT")
  53.  
  54.     /* leave if EOF was reached (stream no longer READY) */
  55.     IF STREAM("SOME.TXT", "S")\="READY" THEN DO
  56.         LEAVE
  57.     END
  58.  
  59.     /* print line */
  60.     SAY "->"line
  61. END
  62.  
  63. CALL STREAM "SOME.TXT", "C", "CLOSE"
  64.  
  65. /* ------------------------------------------------------------------ */
  66.  
  67. EXIT
  68.  
  69.