home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 91 / af091a.adf / af91a3.lzx / prgs / IO / rfile.b < prev    next >
Text File  |  2018-11-21  |  2KB  |  84 lines

  1. {* 
  2. ** A test of ACE's implementation of random files.
  3. **
  4. ** D Benn, 10th,11th,15th March 1996
  5. *}
  6.  
  7. String theName Size 20
  8. String phoneNum Size 15
  9. Shortint age
  10.  
  11. Longint bytesRead
  12.  
  13. Struct person
  14.   String theName Size 20
  15.   String phoneNum Size 16
  16.   Shortint age
  17. End Struct
  18.  
  19. Declare Struct person X,Y
  20.  
  21. Sub HandleBreak
  22.   Print "*** terminating!"
  23.   Stop
  24. End Sub
  25.  
  26. On Break Call HandleBreak : Break On
  27.  
  28. Kill "ram:people.db"
  29.  
  30. {* 
  31. ** Write some people.
  32. ** Error 205 = AmigaDOS error: Object not found. This
  33. ** will be returned if the file does not exist but can
  34. ** be ignored if we want to create a new file.
  35. *}
  36. Open "R",#1,"ram:people.db"
  37. theError = Err
  38. If theError = 0 Or theError = 205 Then
  39.   Repeat
  40.     Read theName
  41.     If theName <> "" Then
  42.       Read phoneNum, age
  43.       X->theName = theName
  44.       X->phoneNum = phoneNum
  45.       X->age = age
  46.       Put #1,X
  47.     End If
  48.   Until theName = ""
  49.  
  50.   {* Test whether we can read a record in the midst of writing. *}
  51.   Get #1,Y,2 : Print "** Seek Test: ";Y->theName;" **" : Print
  52.  
  53.   {* Write a record out beyond the current EOF. *}
  54.   X->theName = "Test"
  55.   X->phoneNum = "911"
  56.   X->age = 96
  57.   Put #1,X,6   
  58.  
  59.   Close #1
  60. End If  
  61.  
  62. Data "David Benn", "444 432", 32
  63. Data "Karen Benn", "327 496", 30
  64. Data "Jessie", "261 461", 2
  65. Data "Marmaduke", "261 461", 6
  66. Data ""
  67.  
  68. {* 
  69. ** Read them back and display them. 
  70. *}
  71. Open "R",#1,"ram:people.db"
  72. If Err = 0 Then
  73.   While Not Eof(1)
  74.     Get #1,X
  75.     bytesRead = Loc(1)
  76.     Print "<<Record";Str$(bytesRead\Sizeof(X));">>"
  77.     Print " Name: ",X->theName
  78.     Print "Phone: ",X->phoneNum
  79.     Print "  Age: ",X->age
  80.     Print
  81.   Wend
  82.   Close #1
  83. End If  
  84.