PTR# and !ABC
Paul Sprangers (346) 524 posts |
It looks like the PTR# command in BASIC doesn’t compile well in !ABC. (The solution, of course, is reading the entire file with BGET#, but I thought it’s worth mentioning here.) |
Stuart Swales (8827) 1357 posts |
Have you tried rewriting it as
[See below] Surely this approach fails if a line that’s read is exactly 255 bytes followed by a terminator: the file pointer will have advanced past the terminator, but you only retract 255 bytes. Or is it the case that BASIC doesn’t look for the terminator if 255 bytes have been read? The documentation isn’t 100% clear to me on that point. Perhaps a different approach would be to take a note of |
Paul Sprangers (346) 524 posts |
It seems so, but actually I’ve no idea. Perhaps none of the files that I’ve read contain strings of exactly 255 bytes. My point however was that it works in BASIC, but fails after compilation.
Although I fail to see how this differs from |
Stuart Swales (8827) 1357 posts |
BASIC does indeed give up once 255 bytes have been read, without looking for a terminator after the last byte. The BASIC Reference Manual could be clearer on this point. |
Rick Murray (539) 13840 posts |
BASIC’s string handling when it comes to over-long strings is bordering on the dangerous. Here’s an artificial example of something that caught me out: >DIM x% 4095 >FOR l% = 0 TO 4095 : x%?l% = RND(26)+65 : NEXT >x%?4095 = 0 >SYS "XOS_GenerateError", x% TO x$ >P. x$, LEN(x$) 0 > It would have been better to either return the first 255 characters, or better yet, to return a “Too long” error. The actual program handled lines read in from a socket connection, and some of the fancier cookies can be kind of lengthy. Of course, I could break it all down and read the data in itty bitty chunks to appease BASIC (the payload is GBPB’ed out of a buffer, it was just header processing)… The BASIC manual doesn’t mention this behaviour either. ;-) |
Martin Avison (27) 1494 posts |
That is true, because it cannot put any more data into the string it is assigning to as 255 is the max length. At that point, if the LEN of the string is 255, you can issue another GET$# to get the next part of the string. I have used that technique occasionally. However, these days with less memory constraints, unless the file is very large I tend to load the whole file into memory using just a OS_File to read the length, DIM a block (LOCAL if possible), the OS_File to read the whole file and process it in memory. Generally much faster. If very large I have done similarly for a reasonaly big buffer-full at a time. |
Paul Sprangers (346) 524 posts |
Strike me, I would never have thought that. And it would have helped me indeed. On the other hand, reading the entire file with BGET# isn’t exactly troublesome either and it always works, no matter how large the strings involved are. But the point was that PTR# gets mangled when compiling. |