Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
INPUT with CHR$(26)
#1
There was someone with a question about reading past CHR$(26) and INPUT over at .com's forum, and then they popped in and asked me a question about it, so I thought I'd write up a quick little demo for it.

Code: (Select All)
Data Steve,50,555-555-0001
Data Pete,70,555-555-0002
Data Bplus,40,555-555-0003
Data Matt,20,555-555-0004
Data EOD

Open "testfile.txt" For Output As #1 'create a data file
Do
    Read dta$
    If dta$ = "EOD" Then Exit Do
    Print #1, dta$;
    If InStr(dta$, "") Then _Continue Else Print #1, ",";
Loop
Close #1

Print "NAME", "AGE", "PHONE"
Open "testfile.txt" For Input As #1
Do
    Do
        Input #1, dta$
        Print dta$,
    Loop Until EOF(1)
    Print
    p = Seek(1)
    If p < LOF(1) Then Seek 1, p + 1 Else Exit Do
Loop

Provided the above works for you guys without glitching (it has embedded CHR$(26) characters that may not copy/paste properly with the forums, it should showcase a file with CHR$(26) Logical EOF markers embedded in it.

If it doesn't copy/paste properly, grab the txt file below and remark out the DATA statements and where we write those to disk.  Just use the provided file and read it without any issues.

Now, when this runs, it reads our data until it finds a CHR$(26) logical EOF marker.  In this case, that EOF *isn't* the actual end of file.  It's simply the end of one stream of data inside that file.  All one has to do is simply SEEK past the current position for that Logical EOF marker and then they can continue on with reading the data as intended.

*WHY* would someone want to do this?

Back in the day, folks were PICKY about such things as disk space and memory and everything else.  When your floppy only held 360k bytes of data, you tried to pack it as tightly as possible.  Instead of writing multiple files of 30ish bytes each, which would use 512 bytes per sector each, folks would pack that info into a single file, which would use the *same* 512 byte sector each.

Other folks would also use CHR$(26) as I have here above -- as data delimiters for tables and such.  It's fairly rare to see CHR$(26) in use today as those memory and disk constraints aren't as restrictive as they used to be, but QB64 has always tried to be backwards compatible and as such it still reads and accepts those CHR$(26) characters as Logical EOF markers.

Easiest work around?  

When EOF is called, *also* check LOF.  If the EOF is triggered and you're not at the EOF, it's a LOGICAL End Of File marker.  Use SEEK to move ahead past it and then keep reading your data according to whatever the heck you're supposed to do with the new stream of info.

That's really all there is to it!  Wink


Attached Files
.txt   testfile.txt (Size: 86 bytes / Downloads: 8)
Reply
#2
Do note that reading files FOR BINARY or RANDOM access don't behave the same.  The above applies to files opened FOR INPUT and not in other modes.  Wink
Reply
#3
[Image: yeah-beavis.gif]

Your program has Do Do in it!

------------------------------------------------------

Cleaver! +1. I have always avoided that with Random files, Open as binary, etc. Especially since we have Line Input with binary reading available in QB64.

For those unwilling to change the way they read files, this is a neat alternative.

... and it's neat, too!

Pete
Reply
#4
Definitely neat.  Did you use the editor to paste the character in?  Would using the CHR$(26) at the end of the data statements work?  The CHR$(26) doesn't even show up as anything readable in most of my text editors.
Reply
#5
(04-13-2025, 02:41 AM)CMR Wrote: Definitely neat.  Did you use the editor to paste the character in?  Would using the CHR$(26) at the end of the data statements work?  The CHR$(26) doesn't even show up as anything readable in most of my text editors.

The QB64 IDE has a couple ways to type the character into your program.  You can use ALT+026 to type it into the IDE.  (Hold ALT, hit 0 then 2 then 6)  Or you can go to the menu for TOOLS and use the ASCII CHART to insert the character directly into your program.

For most other IDEs, you'll probably need to code that a little differently to create the file properly. 

Code: (Select All)
If InStr(dta$, "555") Then
    Print #1, CHR$(26);
Else
    Print #1, ",";
End If

Swap out this line:
If InStr(dta$, "") Then _Continue Else Print #1, ",";

With the above ^:

And you should be good to go in any IDE. I just shortcut the typing as I'm familiar with the QB64PE IDE and tend to use it almost exclusively for most of my stuff.
Reply
#6
Thanks, I tend to use geany because of the cursor movement bug in the ide.
Reply
#7
Cursor movement bug? Does this by chance refer to the cursor position getting lost when it scrolls off the page? That drives me crazy! If that's a bug, I would sure like that fixed - I always figured it was just the way they designed the original QB45 editor back in the day!
Reply
#8
No, it's the one where if you hold down an arrow key to move the cursor, it will still keep moving after you release it.  This may only happen on linux.  I was told by someone on the github it was because glut was used in the backend.  There are supposed to be plans to switch to glfw or something else, and that will fix it.
Reply




Users browsing this thread: 1 Guest(s)