BBC BASIC for Windows
Programming >> Database and Files >> Problem reading last record in a sequential file
http://bb4w.conforums.com/index.cgi?board=database&action=display&num=1407318926

Problem reading last record in a sequential file
Post by PaddyColeman on Aug 6th, 2014, 09:55am

Hi Folks,

I am sure that this is me being thick and missing something obvious in BBC BASIC. I have written a small program that reads through a ZX BASIC source file and removes any REM lines from it. Here is the program:

Code:
      rem +--------------------------------------------------------------------+
      rem |                                                                    |
      rem | ZX BASIC REM REMOVAL PROGRAM                                       |
      rem | İ 2014 PeaSea Software                                             |
      rem |                                                                    |
      rem | Date          Ver    Description                                   |
      rem |                                                                    |
      rem | 05-Aug-2014   1.00   Initial version                               |
      rem |                                                                    |
      rem | 06-Aug-2014   1.01   Updated program version etc.                  |
      rem |                      Changed line to non bright red                |
      rem |                      Changed logo to non bright colours            |
      rem |                      Added check for missing input file            |
      rem |                      Displayed the record counters at the end of   |
      rem |                      the program so zero is shown when the input   |
      rem |                      file is empty                                 |
      rem |                      Changed "Program Completed" to "... Finished" |
      rem |                                                                    |
      rem +--------------------------------------------------------------------+

      rem +------------------+
      rem | Setup the screen |
      rem +------------------+

      mode 17 : off : cls
      colour 15 : print tab(0,0);"ZX BASIC REM REMOVER"
      colour 11 : print tab(33,0);"06-AUG";tab(0,23);"İ 2014";tab(10,23)"PeaSea Software";tab(34,23);"v1.01"
      colour 1 : print tab(0,1);"ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ"
      colour 130 : print tab(7,23);" "
      colour 134 : print tab(8,23);" "
      colour 14 : colour 128
      print tab(0,3);"Input File";tab(2,5);"TIMETABLE-V16.bas";tab(2, 6);"Records read    ="
      print tab(0,8);"Output File";tab(2,10);"STRIPPED.bas";tab(2,11);"Records written ="
      print tab(0,13);"REM File";tab(2,15);"REM.bas";tab(2,16);"Records written ="
      colour 10

      rem +----------------+
      rem | Open the files |
      rem +----------------+

      trace step on

      inFile% = openin("TIMETABLE-V16.bas")

      if inFile% = false then
        colour 1 : print tab(0,18);"ERROR: Input file not found"
        colour 15 : print tab(0,19);"Program Finished"
        end
      endif

      outFile% = openout("STRIPPED.bas")
      rmFile% = openout("REM.bas")

      rem +------------------------------+
      rem | Initialise the file counters |
      rem +------------------------------+

      inLine% = 0
      outLine% = 0
      rmLine% = 0

      rem +-----------------------------------+
      rem | Read the input file until the end |
      rem +-----------------------------------+

      source$ = ""
      read#(inFile%),source$

      while eof#(inFile%) = false
  
        inLine% = inLine% + 1
        print tab(20,6);inLine%
  
        rem +--------------------------------------------------+
        rem | If not a REM - write the line to the output file |
        rem | If a REM - write the line to the REM file        |
        rem +--------------------------------------------------+
  
        if instr(source$," REM ") = false then
          print#(outFile%),source$
          outLine% = outLine% + 1
          print tab(20,11);outLine%
        else
          print#(rmFile%),source$
          rmLine% = rmLine% + 1
          print tab(20,16);rmLine%
        endif
  
        source$ = ""
        read#(inFile%),source$
  
      endwhile

      rem +-----------------+
      rem | Close the files |
      rem +-----------------+

      close#(inFile%)
      close#(outFile%)
      close#(rmFile%)

      print tab(20,6);inLine%;tab(20,11);outLine%;tab(20,16);rmLine%
      colour 15 : print tab(0,19);"Program Finished"

      on : end
 


I have aminated through the code and found that the last line of source in the input file is not read, instead source$ remains empty.

Here is a test input file:

Code:
100 REM \:'\''\''\''\''\''\''\''\''\''\''\''\''\''\''\''\''\''\''\''\''\''\''\''\':
110 REM \: MODEL RAILWAY TIMETABLE\ :
120 REM \: \* 2012 PeaSea Software \ :
130 REM \: 1.0 : 12-NOV-2010      \ :
140 REM \: 1.1 : 26-MAY-2011      \ :
150 REM \: 1.2 : 25-JUL-2011      \ :
160 REM \: 1.3 : 28-DEC-2011      \ :
240 GO TO 3820
250 REM 
260 REM Class|UIC/Whytes|No|Name|Power (D,MT,P,F)|Livery
270 REM 
280 LET a$="": IF i$(34)<"X" THEN LET a$=STR$  CODE i$(34)+"/"
290 IF i$(35)<"X" THEN LET a$=a$+STR$  CODE i$(35)+"MT"
300 IF i$(36)<"X" THEN LET a$=a$+STR$  CODE i$(36)+"P"
310 IF i$(37)<"X" THEN LET a$=a$+STR$  CODE i$(37)+"F"
320 LPRINT AT 9,l;v$( CODE i$(1),2 TO 5);AT 32,l;v$( CODE i$(3)+v(2),2 TO 8);AT 67,l;i$(4 TO 8);AT 97,l;i$(10 TO 32);AT 197,l;a$;AT 227,l;v$(v(3)+INT ( CODE i$(33)/16),2 TO 3): RETURN 
330 LET a=1 


So in this case line 330 is not read by the BBC BASIC program.

Any ideas?

Many thanks

Paddy

Re: Problem reading last record in a sequential fi
Post by PaddyColeman on Aug 6th, 2014, 10:00am

Ah, I think the solution may be to use GET$# rather than READ# as the input file is probably line terminated with CR/LF - I will check and test.

Many thanks

Paddy

Re: Problem reading last record in a sequential fi
Post by rtr on Aug 6th, 2014, 12:47pm

on Aug 6th, 2014, 10:00am, PaddyColeman wrote:
Ah, I think the solution may be to use GET$# rather than READ# as the input file is probably line terminated with CR/LF - I will check and test.

Yes, GET$# will return the remaining characters in the file, even if no terminator is present, whereas INPUT# (and READ#) will return an empty string if the CR terminator is not found.

Check out the FNreadline() function here:

http://bb4w.wikispaces.com/Reading+and+writing+plain+text+files

Richard.
Re: Problem reading last record in a sequential fi
Post by PaddyColeman on Aug 6th, 2014, 1:05pm

on Aug 6th, 2014, 12:47pm, Richard Russell wrote:
Yes, GET$# will return the remaining characters in the file, even if no terminator is present, whereas INPUT# (and READ#) will return an empty string if the CR terminator is not found.

Check out the FNreadline() function here:

http://bb4w.wikispaces.com/Reading+and+writing+plain+text+files

Richard.


Thanks Richard. Just tried GET$# and this does not work either as my input file is terminated with CR/LF (&0D0A).

I will check out the function you highlighted to see if that helps.

Many thanks

Paddy

Re: Problem reading last record in a sequential fi
Post by PaddyColeman on Aug 6th, 2014, 1:20pm

Hi Richard,

Thank you for the link and this seems to be what I need for my particular input file format. However, the IDE will not allow me to enter:

Code:
      def fn_readline(f%)
      local a$
      repeat
        a$=get$#(f%)
      until a$<>"" or eof#(f%)
      =a$ 


The "def" part seems not to be accepted. I have tried defining the function at the beginning and end of the source program.

Any ideas?

Many thanks

Paddy

Re: Problem reading last record in a sequential fi
Post by PaddyColeman on Aug 6th, 2014, 1:29pm

Tried putting the function at the end and it seems to be working now. The DEF bit is still highlighted as a string but may be that is just an IDE funny.

Many thanks

Paddy

Re: Problem reading last record in a sequential fi
Post by PaddyColeman on Aug 6th, 2014, 1:54pm

Works fine - thanks Richard. To close the thread here is my completed wee program.

Code:
      rem +-------------------------------------------------------------------+
      rem |                                                                   |
      rem | Program : ZX BASIC REM REMOVAL                                    |
      rem | Author  : İ 2014 PeaSea Software                                  |
      rem |                                                                   |
      rem | Date          Ver    Description                                  |
      rem |                                                                   |
      rem | 05-Aug-2014   1.00   Initial version                              |
      rem |                                                                   |
      rem | 06-Aug-2014   1.01   Updated program version etc.                 |
      rem |                      Changed line to non bright red               |
      rem |                      Changed logo to non bright colours           |
      rem |                      Added check for missing input file           |
      rem |                      Displayed record counters at program end     |
      rem |                      Changed "Program Completed" to "...Finished" |
      rem |                      Read/write code now supports CR & LF         |
      rem |                                                                   |
      rem +-------------------------------------------------------------------+

      rem +-------------------------------------------------------------------+
      rem | Setup the screen                                                  |
      rem +-------------------------------------------------------------------+

      mode 17 : off : cls
      colour 15 : print tab(0,0);"ZX BASIC REM REMOVER"
      colour 11 : print tab(33,0);"06-AUG";tab(0,23);"İ 2014";tab(10,23)"PeaSea Software";tab(34,23);"v1.01"
      colour 1 : print tab(0,1);"ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ"
      colour 130 : print tab(7,23);" "
      colour 134 : print tab(8,23);" "
      colour 14 : colour 128
      print tab(0,3);"Input File";tab(2,5);"TIMETABLE-V16.bas";tab(2, 6);"Records read    ="
      print tab(0,8);"Output File";tab(2,10);"STRIPPED.bas";tab(2,11);"Records written ="
      print tab(0,13);"REM File";tab(2,15);"REM.bas";tab(2,16);"Records written ="
      colour 10

      rem +-------------------------------------------------------------------+
      rem | Open the files                                                    |
      rem +-------------------------------------------------------------------+

      rem trace step on

      inFile% = openin("TIMETABLE-V16.bas")

      if inFile% = false then
        colour 1 : print tab(0,18);"ERROR: Input file not found"
        colour 15 : print tab(0,19);"Program Finished"
        end
      endif

      outFile% = openout("STRIPPED.bas")
      rmFile% = openout("REM.bas")

      rem +-------------------------------------------------------------------+
      rem | Initialise the record counters                                    |
      rem +-------------------------------------------------------------------+

      inLine% = 0
      outLine% = 0
      rmLine% = 0

      rem +-------------------------------------------------------------------+
      rem | Read the input file until the end                                 |
      rem | FN_ReadLineCRLF reads a non blank line from the text file         |
      rem | Write the line appending a LF (&0A) to the end                    |
      rem +-------------------------------------------------------------------+

      source$ = fn_ReadLineCRLF(inFile%)

      while eof#(inFile%) = false
  
        inLine% = inLine% + 1
        print tab(20,6);inLine%
  
        rem +-----------------------------------------------------------------+
        rem | If not a REM - write the line to the output file                |
        rem | If a REM - write the line to the REM file                       |
        rem +-----------------------------------------------------------------+
  
        if instr(source$," REM ") = false then
          print#(outFile%),source$
          bput#(outFile%),&0A
          outLine% = outLine% + 1
          print tab(20,11);outLine%
        else
          print#(rmFile%),source$
          bput#(rmFile%),&0A
          rmLine% = rmLine% + 1
          print tab(20,16);rmLine%
        endif
  
        source$ = fn_ReadLineCRLF(inFile%)
  
      endwhile

      rem +-------------------------------------------------------------------+
      rem | Close the files                                                   |
      rem +-------------------------------------------------------------------+

      close#(inFile%)
      close#(outFile%)
      close#(rmFile%)

      rem +-------------------------------------------------------------------+
      rem | Display record counters and finish                                |
      rem +-------------------------------------------------------------------+

      print tab(20,6);inLine%;tab(20,11);outLine%;tab(20,16);rmLine%
      colour 15 : print tab(0,19);"Program Finished"

      on : end

      rem +-------------------------------------------------------------------+
      rem | Function : _ReadLineCRLF                                          |
      rem | Purpose  : Reads a line from a text file that is terminated by CR |
      rem |            and LF.  Any blank lines are ignored.                  |
      rem +-------------------------------------------------------------------+

      def fn_ReadLineCRLF(f%)
      local a$
      repeat
        a$=get$#(f%)
      until a$<>"" or eof#(f%)
      =a$ 


Many thanks

Paddy

Re: Problem reading last record in a sequential fi
Post by rtr on Aug 6th, 2014, 2:22pm

on Aug 6th, 2014, 1:29pm, PaddyColeman wrote:
The DEF bit is still highlighted as a string but may be that is just an IDE funny.

DEF is highlighted (by default) in red and strings in magenta. If they look the same to you perhaps you suffer from a degree of colour-blindness. tongue

You can always change the colours, to suit your personal preference: 'Options... Set Colours...' menu selection.

Richard.
Re: Problem reading last record in a sequential fi
Post by PaddyColeman on Aug 6th, 2014, 2:54pm

Thanks Richard - lots to learn. grin

Thank you for your excellent support though.

Kind regards

Paddy