home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / basic / compiler / asic / filedemo.asi < prev    next >
Text File  |  1994-03-01  |  4KB  |  137 lines

  1. REM FILEDEMO.ASI
  2.  
  3. REM This is an ASIC demo program which illustrates how to perform various file
  4. REM operations.
  5.  
  6. REM Part I - Creates an ASCII file easily readable by GWBASIC/BASICA and others 
  7. REM Part II - Reads an ASCII file with strings delimited by CR/LF
  8. REM Part III - Creates a 5 record random file and allows you to display and
  9. REM            update any record.
  10. REM Part IV - Appends a record using the OPEN statement with Append option
  11. REM Part V  - Appends a record to a file OPEN in random I/O mode ("R")
  12. REM           and then lists all records in the file.
  13.  
  14. REM Note:  This sample code does not check the ERROR system variable after
  15. REM        most OPEN/CLOSE/INPUT#/PRINT# statements.  However, when writing
  16. REM        "real" programs you should always add a statement to check for
  17. REM        errors, such as "IF ERROR>0 THEN PROCESSERROR:" after EVERY
  18. REM        OPEN/CLOSE/INPUT#/PRINT# statement.
  19.  
  20.  
  21. REM Part I - Creating a file readable by GWBASIC/BASICA and other programs.
  22. REM Write file FILEDEMO.001 with a string and integer delimited by comma,
  23. REM with a carriage return/line feed sequence at the end
  24. CR$=CHR$(13)
  25. LF$=CHR$(10)
  26. CRLF$=CR$+LF$
  27. OPEN "O", 1,"FILEDEMO.001"
  28. APPLES=100
  29. PRINT #1, "Apples" NONULL
  30. APPLES$=STR$(APPLES)
  31. PRINT #1,"," NONULL
  32. PRINT #1, APPLES$ NONULL
  33. PRINT #1, CRLF$ NONULL
  34. CLOSE 1
  35.  
  36. REM Part II - Reading a "normal" ASCII DOS file (CR/LF delimited).
  37. REM Reads FILEDEMO.001 which was written in part I, and displays it's contents. 
  38. OPEN "I", 1, "FILEDEMO.001"
  39. INPUT #1,APPLES$ CRLF
  40. PRINT APPLES$
  41. CLOSE 1
  42.  
  43. REM PART III - Using a random file
  44. REM  Create a file FILEDEMO.002
  45. OPEN "O",1,"FILEDEMO.002"
  46. REM Create five "records" with a single integer field, set initial values 1-5.
  47. REM File is written in normal ASIC format for integers (2 binary bytes).
  48. FOR I=1 TO 5
  49.    PRINT #1,i
  50. NEXT I
  51. CLOSE 1
  52. REM Process FILEDEMO.002 randomly
  53. REM Prompt for a record to modify, show the current value, accept a new value
  54. REM from the user, and then write the new value.
  55. OPEN "R",1,"FILEDEMO.002"
  56. GETRECNO:
  57.    PRINT "ENTER RECORD # TO MODIFY";
  58.    INPUT RECNO
  59.    IF RECNO<1 THEN
  60.        PRINT "RECORD # MUST BE GREATER THAN 0"
  61.        GOTO GETRECNO:
  62.    ELSE
  63.        IF RECNO>5 THEN
  64.            PRINT "RECORD # MUST BE LESS THAN 5"
  65.            GOTO GETRECNO:
  66.        ENDIF
  67.    ENDIF
  68. FILEOFFSET=RECNO*2
  69. FILEOFFSET=FILEOFFSET - 2
  70. REM FILEOFFSET NOTES:  In this example, each record contains one integer field
  71. REM that is two bytes long.  Must subtract two since first record starts at
  72. REM offset=0.
  73. NEWPOS=FILEPOS(1,FILEOFFSET)
  74. INPUT #1, VALUE
  75. PRINT "CURRENT VALUE IS ";
  76. PRINT VALUE
  77. PRINT "ENTER NEW VALUE ";
  78. INPUT VALUE
  79. NEWPOS=FILEPOS(1,FILEOFFSET)
  80. PRINT #1, VALUE
  81. CLOSE 1
  82.  
  83. REM PART IV - Append a record to the random file FILEDEMO.002
  84. REM Append option 1 - Use OPEN with the "A" option if the file is not open.
  85. REM Add record #6 with a value of 99
  86. OPEN "A",1,"FILEDEMO.002"
  87. PRINT #1, 99
  88. CLOSE 1
  89.  
  90. REM Append option 2 - If file is already open random ("R"), use the file pos to 
  91. REM move to the end of the file, and then write the record
  92. REM add record # 7
  93. OPEN "R",1,"FILEDEMO.002"
  94. X=FILEPOS(1,EOF)
  95. PRINT #1, 999
  96.  
  97. REM Now position file to beginning, and display all values by reading file
  98. REM sequentially
  99. NEWPOS=FILEPOS(1,0)
  100. RECNO=0
  101. READALL:
  102.    INPUT #1,VALUE
  103.    IF ERROR>0 THEN
  104.        PRINT "File Status = ";
  105.        PRINT ERROR
  106.        PRINT "Records Read = ";
  107.        PRINT RECNO
  108.        CLOSE 1
  109.        GOTO EXIT:
  110.    ENDIF
  111.    RECNO=RECNO+1
  112.    PRINT "Record # ";
  113.    PRINT RECNO;
  114.    PRINT " ";
  115.    PRINT VALUE
  116.    GOTO READALL:
  117. EXIT: REM clean up disk
  118.    PRINT "Do you want to delete the work files created by this program"
  119.    print "'FILEDEMO.001 and FILEDEMO.002' (Y/N)";
  120.    INPUT x$
  121.    x$=UCASE$(x$)
  122.    IF x$="Y" THEN
  123.        KILL "filedemo.001"
  124.        KILL "filedemo.002"
  125.        PRINT "Work files deleted"
  126.    ENDIF
  127.    END
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.