home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / BASIC / POWBASIC / LIBRARY1 / EXAMP1.ZIP / RMEX195.BAS < prev    next >
BASIC Source File  |  1990-06-28  |  3KB  |  100 lines

  1. FUNCTION PForKey$(Msg$)
  2.   'Waits for a keystroke and returns value to caller.
  3.   PRINT Msg$, "Press any key to continue..."
  4.   PRINT
  5.   WHILE NOT INSTAT:WEND
  6.   PForKey$ = INKEY$
  7. END FUNCTION
  8.  
  9. SUB SequentialOutput
  10.   'The file is opened for sequential output,
  11.   'and some data is written to it.
  12.   KeyP$ = PForKey$("Now for some sequetial output")
  13.   OPEN "OPEN.DTA" FOR OUTPUT AS #1
  14.   IntegerVar% = 12345
  15.   TempStr$ = "History is made at night."
  16.   WRITE #1, TempStr$, IntegerVar% * 2, TempStr$, IntegerVar% \ 2
  17.   CLOSE 1
  18. END SUB
  19.  
  20. SUB SequentialAppend
  21.   'The file is opened for sequential output, and
  22.   'data in this case is added to the end of file.
  23.   KeyP$ = PForKey$("Now to append some more stuff")
  24.   OPEN "OPEN.DTA" FOR APPEND AS #1
  25.   IntegerVar% = 32123
  26.   TempStr$ = "I am not a number!"
  27.   WRITE #1, TempStr$, IntegerVar% * 0.2
  28.   CLOSE 1
  29. END SUB
  30.  
  31. SUB SequentialInput
  32.   'The file is opened for sequential input,
  33.   'and data read is displayed onscreen
  34.   KeyP$ = PForKey$("Now to read it back" )
  35.   OPEN "OPEN.DTA" FOR INPUT AS #1
  36.   LINE INPUT #1, TempStr$
  37.   PRINT TempStr$
  38.   PRINT
  39.   TempStr$ = ""
  40.   WHILE NOT EOF(1)
  41.     TempStr$ = TempStr$ + INPUT$(1,1)
  42.   WEND
  43.   PRINT TempStr$
  44.   PRINT
  45.   CLOSE 1
  46.   KeyP$ = PForKey$("")
  47. END SUB
  48.  
  49. SUB BinaryIO
  50.   'The file is opened for Binary I/O.  Data is read using
  51.   'GET$.  SEEK explicitly moves the file pointer to the end
  52.   'of file, and the same data is written back to the file.
  53.   KeyP$ = PForKey$("Now for Binary input and output" )
  54.   OPEN "OPEN.DTA" FOR BINARY AS #1
  55.   TempStr$ = ""
  56.   WHILE NOT EOF(1)
  57.     GET$ 1,1,Char$
  58.     TempStr$ = TempStr$ + Char$
  59.   WEND
  60.   PRINT TempStr$
  61.   PRINT
  62.   SEEK 1, LOF(1)
  63.   FOR I% = 1 TO LEN( TempStr$ )
  64.     PUT$ 1, MID$( TempStr$, I%, 1 )
  65.   NEXT I%
  66.   CLOSE 1
  67.   KeyP$ = PForKey$("")
  68. END SUB
  69.  
  70. SUB RandomIO
  71.   'Open file for random I/O.  Use MAP to declare a buffer to
  72.   'hold the data that is written and read.  GET and PUT read
  73.   'and write the data.  Note that before GET is performed, the
  74.   'data to be stored in the file's buffer is assigned to the
  75.   'MAPped flex string variable.
  76.   KeyP$ = PForKey$("Now for some random I/O" )
  77.   OPEN "OPEN.DTA" AS #1 LEN = 1
  78.   MAP 1,1 AS Char$$
  79.   TempStr$ = ""
  80.   TempSize% = LOF(1)
  81.   'using GET, read in the entire file
  82.   FOR I% = 1 to TempSize%
  83.     GET 1, I%
  84.     TempStr$ = TempStr$ + Char$$
  85.   NEXT I%
  86.   'PUT copies the data in reverse into the random-access file.
  87.   FOR I% = LEN( TempStr$ ) TO 1 STEP -1
  88.     Char$$ = MID$(TempStr$, I%, 1 )
  89.     PUT 1, LOF(1)
  90.   NEXT I%
  91.   CLOSE 1
  92. END SUB
  93.  
  94. CLS
  95. CALL SequentialOutput
  96. CALL SequentialAppend
  97. CALL SequentialInput
  98. CALL BinaryIO
  99. CALL RandomIO
  100. END