User Tools

Site Tools


finding_20the_20last_20character_20read_20from_20a_20file

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

finding_20the_20last_20character_20read_20from_20a_20file [2018/03/31 13:19]
127.0.0.1 external edit
finding_20the_20last_20character_20read_20from_20a_20file [2018/04/17 16:10] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Richard Russell, January 2012//\\ \\  Occasionally you may want to find out what was the last character read from a file (without the overhead of backspacing the file pointer and reading it again). For example, if you read a string from a file using [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin5.html#​getfile|GET$#​]] it may be useful to know whether the string was terminated by **CR**, **LF** or **NUL**.\\ \\  Because of the way //BBC BASIC for Windows// buffers files in memory (to increase speed of access) it is possible to discover the last character read by examining the contents of the buffer. This function does it:\\  //by Richard Russell, January 2012//\\ \\  Occasionally you may want to find out what was the last character read from a file (without the overhead of backspacing the file pointer and reading it again). For example, if you read a string from a file using [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin5.html#​getfile|GET$#​]] it may be useful to know whether the string was terminated by **CR**, **LF** or **NUL**.\\ \\  Because of the way //BBC BASIC for Windows// buffers files in memory (to increase speed of access) it is possible to discover the last character read by examining the contents of the buffer. This function does it:\\ 
 +<code bb4w>
         DEF FNlastfilechar(F%)         DEF FNlastfilechar(F%)
         LOCAL B%, B&         LOCAL B%, B&
Line 8: Line 9:
         B& = ?​(^@hfile%(F%) + 32) - 1         B& = ?​(^@hfile%(F%) + 32) - 1
         = B%?B&         = B%?B&
 +</​code>​
 The parameter is the file channel number (as returned from [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin6.html#​openin|OPENIN]] or [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin6.html#​openup|OPENUP]]) and the value returned is the last character read or **-1** if the supplied channel number is invalid. If an attempt was made to read beyond the end of the file, the returned value is undefined.\\ \\  The function will only work reliably if the last operation on the file was a read (e.g. using **BGET#**, **GET$#** or **INPUT#​**). It can be used only with //files//, not with //ports//. The parameter is the file channel number (as returned from [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin6.html#​openin|OPENIN]] or [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin6.html#​openup|OPENUP]]) and the value returned is the last character read or **-1** if the supplied channel number is invalid. If an attempt was made to read beyond the end of the file, the returned value is undefined.\\ \\  The function will only work reliably if the last operation on the file was a read (e.g. using **BGET#**, **GET$#** or **INPUT#​**). It can be used only with //files//, not with //ports//.
finding_20the_20last_20character_20read_20from_20a_20file.txt ยท Last modified: 2018/04/17 16:10 by tbest3112