home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l320 / 2.img / EXAMPLES / BINWRITE.F < prev    next >
Encoding:
Text File  |  1989-10-12  |  7.0 KB  |  203 lines

  1. c This is an example of how to write sequentially to a file
  2. c without the bothersome 4-byte header and footer information.
  3. c Written by David Rapier, MicroWay, Inc.
  4. c Permission to use text and ideas in this file is granted.
  5. c
  6.     Program noheader
  7.  
  8. c L is the size of the buffer being used in the direct access file.
  9. c It may be set to any number.  The larger L is, the fewer times
  10. c the disk will be read.  The smaller L is, the less the file will
  11. c be padded at the end (the file will be padded with blanks at the
  12. c end).
  13. c
  14.     Parameter (L=512)
  15.  
  16. c The program will write three pieces of information to the file:
  17. c a 400-byte string, a 200-byte string, and a 10-byte number.
  18. c These are just examples -- the subroutine will handle any size
  19. c string.
  20. c
  21.     character*400 A400
  22.     character*200 A200
  23.     character*10  A10
  24.     Data A400 / 'This is a 400-byte piece of information.' /
  25.     Data A200 / 'This is a 200-byte piece of information.' /
  26.  
  27. c The open statement may be on any legal unit number.
  28. c It must include ACCESS='DIRECT' and RECL=L.
  29. c
  30.     open (7,file='fort.7',status='new',access='direct',recl=L)
  31.  
  32. c Writing character strings to the file is straightforward.
  33. c Pass the number of the file (must be the same throughout)
  34. c and the actual string to be written.
  35. c
  36.     Call BIN_WRITE(7,a400)
  37.     Call BIN_WRITE(7,a200)
  38.  
  39. c To write numbers to the file, convert them first to strings.
  40. c This can be done using an internal write statement.
  41. c
  42.     X = 17.0
  43.     WRITE(A10,900) X
  44. 900    Format(F9.3)
  45.  
  46. c Then pass the resulting string variable to the subroutine.
  47. c
  48.     Call BIN_WRITE(7,a10)
  49.  
  50. c To finish writing the file, pass "char(0)" as an argument.
  51. c This is necessary to flush the buffer.
  52. c
  53.     Call BIN_WRITE(7,char(0))
  54.     Close (7)
  55.     End
  56.  
  57. c---------------------------------------------------------------c
  58. c Subroutine BIN_WRITE handles all WRITEs to the designated file.
  59. c Input arguments:  FILENUM = Integer*4, file number.
  60. c                   CHARSTR = Character string to be written
  61. c
  62. c Variables: BUFFER  = Character string buffer
  63. c            CONNECT = Connect status to ensure FILENUM is really open.
  64. c            FILECONST Check to ensure that FILENUM is the same
  65. c                      throughout the program.
  66. c            IO      = Check during write to ensure IOSTAT is okay.
  67. c            L       = Length of buffer.  Same as in main program.
  68. c            LENGTH  = Length of character string passed.
  69. c            NUMBER  = Number of times this routine is called.
  70. c            POINTER = Shows place in buffer.
  71. c            RECORD  = Number of record presently being filled.
  72. c            TEMP    = Temporary pointer during write.
  73. c
  74. c Output:    Incoming arguments are never changed.
  75. c            Either the write will occur or program will halt.
  76. c
  77.     Subroutine    BIN_WRITE( FILENUM,CHARSTR )
  78.     Parameter     (L=512)
  79.     Integer*4     POINTER, NUMBER, LENGTH, RECORD, IO, TEMP
  80.     Integer*4     FILENUM, FILECONST
  81.     Character*(L) BUFFER
  82.     Character*(*) CHARSTR
  83.     Logical*4     CONNECT
  84.     Data POINTER, NUMBER, RECORD, FILECONST / 0, 0, 0, 0 /
  85.     Data BUFFER / ' ' /
  86.  
  87. c Increment number of times routine is called.  This variable is used
  88. c in error messages to help debug the main program.
  89. c
  90.     NUMBER = NUMBER + 1
  91.  
  92. c Although LENGTH starts out as the length of the string, it ends
  93. c up as the length remaining to write, if the string is larger than
  94. c the buffer being written into.
  95. c
  96.     LENGTH = len(CHARSTR)
  97.  
  98. c FILENUM will differ from FILECONST in two situations.
  99. c (1) First time the subroutine is called.
  100. c     If FILECONST=0 this is true.  In this case,
  101. c     FILECONST is set equal to FILENUM and the status of the
  102. c     file number is checked.  If the file is not open, a message
  103. c     is given and the file is properly opened, with default
  104. c     file name.
  105. c (2) User is trying to use subroutine for TWO files.
  106. c     In this case, an error message is given with information to
  107. c     help the programmer find the problem.
  108. c
  109.     If (FILENUM.ne.FILECONST) then
  110.       If (FILECONST.eq.0) then
  111.         FILECONST = FILENUM
  112.         Inquire(FILENUM,opened=CONNECT)
  113.         If (.not. CONNECT) then
  114.           Print "(' *** OPENING FILE #',i4,'***')", FILENUM
  115.           Print "(' [Call #   1 of BIN_WRITE]')"
  116.           Open(FILENUM, Status='Direct',Recl=L)
  117.          End if
  118.        else
  119.         Print "(' This subroutine is designed to allow binary')"
  120.         Print "(' access to ONE file number only.  File number')"
  121.         Print "(' is INCONSISTANT on call number',i4)", NUMBER
  122.         Print "(' of BIN_WRITE.  Please change program.')"
  123.         Print "(' File number     =',i4)", FILENUM
  124.         Print "(' Number expected =',i4)", FILECONST
  125.         Stop
  126.        End if
  127.      End if
  128.  
  129. c These lines check to ensure the file is still open.
  130. c If the file is closed, it will be reopened using a default
  131. c file name and a warning message will be given.
  132. c
  133.     inquire(FILENUM,opened=CONNECT)
  134.     if (.NOT. CONNECT) then
  135.       Print "(' *** REOPENING FILE #',i4,'***')", FILENUM
  136.       Print "(' [Call #',i4,'of BIN_WRITE]')", NUMBER
  137.       Open(FILENUM, Status='Direct',Recl=L)
  138.      End If
  139.  
  140. c CHARSTR=char(0) will flush the buffer and prepare the file for
  141. c closing.  All write statements check iostatus, error status.
  142. c
  143.     if (LENGTH.eq.1 .AND. CHARSTR.eq.char(0)) then
  144.       BUFFER(POINTER+1:L) = ' '
  145.       RECORD = RECORD + 1
  146.       POINTER = 0
  147.       Write (FILENUM, iostat=IO, err=100, rec=RECORD) BUFFER
  148.       if (IO .ne. 0) GoTo 101
  149.       Return
  150.      end if
  151.  
  152. c TEMP is the amount of CHARSTR already written.
  153. c TEMP + LENGTH will always be the length of CHARSTR.
  154. c
  155.     TEMP = 0
  156.  
  157. c POINTER is how much buffer is already filled.
  158. c L-POINTER is how much buffer is left.
  159. c If LENGTH is greater than L-POINTER, part of CHARSTR
  160. c is read into BUFFER, BUFFER is written, and
  161. c the comparison is made again.
  162. c
  163.     do 10 while (LENGTH.gt.L-POINTER)
  164.       BUFFER(POINTER+1:L) = CHARSTR(TEMP+1:TEMP+L-POINTER)
  165.       RECORD = RECORD + 1
  166.       TEMP = TEMP+L-POINTER
  167.       LENGTH = LENGTH-L+POINTER
  168.       POINTER = 0
  169.       Write (FILENUM, iostat=IO, err=100, rec=RECORD) BUFFER
  170.       if (IO .ne. 0) GoTo 101
  171. 10     Continue
  172.  
  173. c By this point, the amount left in CHARSTR is either
  174. c smaller than the buffer or the same length.
  175. c If smaller, then the buffer is partially filled.
  176. c If the same length, the buffer is filled and writes to disk.
  177. c
  178.     If (LENGTH.lt.L-POINTER) then
  179.       BUFFER(POINTER+1:POINTER+LENGTH) = CHARSTR(TEMP+1:TEMP+LENGTH)
  180.       POINTER = POINTER+LENGTH
  181.      else
  182.       BUFFER(POINTER+1:L) = CHARSTR(TEMP+1:TEMP+L-POINTER)
  183.       POINTER = 0
  184.       RECORD = RECORD + 1
  185.       Write (FILENUM, iostat=IO, err=100, rec=RECORD) BUFFER
  186.       If (IO .ne. 0) GoTo 101
  187.      End if
  188.     Return
  189.  
  190. c These are diagnostic lines in case any write statement
  191. c gives an error.
  192. c
  193. 101    Print "(' IOSTAT ERROR.  IOSTAT =',i4)",   IOSTAT
  194. 100    Print "(' Error in invocation #',i4,$)",   NUMBER
  195.     Print "(' of WRITE to File #',i4,':')" ,   FILENUM
  196.     Print "(' Record #',i4,', Pointer =',i4)", RECORD, POINTER
  197.     Print "(' Length =',i4,', ',$)",           LEN(CHARSTR)
  198.     Print "(' Remaining length =',i4)",        LENGTH
  199.     Print "(' Buffer size =',i4,', ',$)",      L
  200.     Print "(' TEMP =',i4)",                    TEMP
  201.     Stop
  202.     End
  203.