TIP: Click on subject to list as thread! ANSI
echo: power_bas
to: STEVE LEGG
from: JOE NEGRON
date: 1997-04-17 18:44:00
subject: Binary LINE INPUT #

** Quoting Steve Legg to All:
SL> I've been working on a BINARY line input routine, which I need for
  > reading UNIXformatted text files.  (The difference is the format of
  > the EOL character, carriage return-line feed for DOS text files, and
  > line feed only for UNIX text files.)
Try adapting the following - it's from Ethan Winer's BASIC Techniques
and Utilities and claims to be
================================ Begin =================================
'********* BUFIN.BAS - fast LINE INPUT replacement
'Copyright (c) 1992 Ethan Winer
DEFINT A-Z
DECLARE FUNCTION BufIn$ (FileName$, Done)
LINE INPUT "Enter a file name: ", FileName$
Start! = TIMER
DO
   This$ = BufIn$(FileName$, Done)
   IF Done THEN EXIT DO
LOOP
Done! = TIMER
PRINT "Buffered input: "; Done! - Start!
Start! = TIMER
OPEN FileName$ FOR INPUT AS #1
DO
   LINE INPUT #1, This$
LOOP UNTIL EOF(1)
Done! = TIMER
PRINT " BASIC's INPUT: "; Done! - Start!
CLOSE
END
FUNCTION BufIn$ (FileName$, Done) STATIC
IF NOT Reading THEN            'if the first time through
  Reading = -1                 'show that we're now reading
  Done = 0                     'clear Done just in case
  CR = 0                       'no return found yet.
  CR$ = CHR$(13)               'define for speed later
  FileNum = FREEFILE           'open the file
  OPEN FileName$ FOR BINARY AS #FileNum
  Remaining& = LOF(FileNum)    'byte count to be read
  BufSize = 4096               'bytes to read each pass
  Buffer$ = SPACE$(BufSize)    'assume BufSize bytes
END IF
'---- This is the main outer loop.
DO WHILE Remaining&              'while more in the file
  IF CR = 0 THEN                 'if no Return was found
    IF Remaining& < BufSize THEN 'read only what remains
      BufSize = Remaining&       'resize the buffer
      IF BufSize < 1 THEN EXIT DO'possible only if EOF 26
      Buffer$ = SPACE$(BufSize)  'create the file buffer
    END IF
    GET #FileNum, , Buffer$      'read a block
    BufPos = 1                   'start at the beginning
  END IF                         '  of that block
 
  DO                                 'walk through buffer
    CR = INSTR(BufPos, Buffer$, CR$) 'look for a Return
    IF CR THEN                       'we found one
      SaveCR = CR                    'save where
      BufIn$ = MID$(Buffer$, BufPos, CR - BufPos)
      BufPos = CR + 2                'skip inevitable LF
      EXIT FUNCTION                  'all done for now
    ELSE                             'back up in the file
      '---- If we reached the end of the file and no 13
      '     was found, return what remains in the string.
      IF SEEK(FileNum) >= LOF(FileNum) THEN
        Output$ = MID$(Buffer$, SaveCR + 2)
        '---- Trap a trailing CHR$(26) EOF marker.
        IF RIGHT$(Output$, 1) = CHR$(26) THEN
          Output$ = LEFT$(Output$, LEN(Output$) - 1)
        END IF
        BufIn$ = Output$             'assign the function
        Remaining& = BufSize         'set to fall out
        EXIT DO                      'and exit now
      END IF
      Slop = BufSize - SaveCR - 1    'calc buffer excess
      Remaining& = Remaining& + Slop 'calc file excess
      SEEK #FileNum, SEEK(FileNum) - Slop  'seek to start
    END IF
  LOOP WHILE CR                'while more in buffer
  Remaining& = Remaining& - BufSize
LOOP
Reading = 0                    'we're not reading anymore
Done = -1                      'show that we're all done
CLOSE #FileNum                 'final cleanup
END FUNCTION
================================= End ==================================
            --From Bay Ridge, Brooklyn, NY, Thu, 04-17-97--
                     --Internet: jnegron@ibm.net--
--- QScan/PCB v1.19b / 01-0154
---------------
* Origin: MicroLink BBS * Dinuba, CA 209-591-8753 (1:214/80)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.