Author |
Topic: Problem reading last record in a sequential file (Read 1434 times) |
|
rtr
Guest
|
|
Re: Problem reading last record in a sequential fi
« Reply #2 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.
|
|
Logged
|
|
|
|
PaddyColeman
New Member
member is offline
Gender:
Posts: 7
|
|
Re: Problem reading last record in a sequential fi
« Reply #3 on: Aug 6th, 2014, 1:05pm » |
|
on Aug 6th, 2014, 12:47pm, Richard Russell wrote:
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
|
|
Logged
|
|
|
|
PaddyColeman
New Member
member is offline
Gender:
Posts: 7
|
|
Re: Problem reading last record in a sequential fi
« Reply #4 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
|
|
|
|
PaddyColeman
New Member
member is offline
Gender:
Posts: 7
|
|
Re: Problem reading last record in a sequential fi
« Reply #5 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
|
|
Logged
|
|
|
|
PaddyColeman
New Member
member is offline
Gender:
Posts: 7
|
|
Re: Problem reading last record in a sequential fi
« Reply #6 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
|
|
|
|
rtr
Guest
|
|
Re: Problem reading last record in a sequential fi
« Reply #7 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.
You can always change the colours, to suit your personal preference: 'Options... Set Colours...' menu selection.
Richard.
|
|
Logged
|
|
|
|
PaddyColeman
New Member
member is offline
Gender:
Posts: 7
|
|
Re: Problem reading last record in a sequential fi
« Reply #8 on: Aug 6th, 2014, 2:54pm » |
|
Thanks Richard - lots to learn.
Thank you for your excellent support though.
Kind regards
Paddy
|
|
Logged
|
|
|
|
|