Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extended KotD #8: _READFILE$
#1
This is another of the latest commands, which came into the language back in version 3.12.   Let's take a moment and work out what it does for us, and how much coding it can save for us, easily.

Before this command was added into the language, if we wanted to read a whole file into a string, we'd have to do something similar to the following:

Code: (Select All)
Open "temp.txt" For Binary As #1 'open the file
temp$ = Space$(LOF(1)) '          set a string to the proper size to hold the whole file
Get #1, 1, temp$ '                get the data from the file
Close #1 '                        close that file and free that filehandle

So this isn't exactly the longest, or hardest code to write.  The main thing one has to watch for here is:
1) Does the file exist?  If not, then you probably just created a "temp.txt" of size 0 on your drive.  Whoops!
2) Are you trying to open the file with an existing handle?  If so, error....
3) Did you set the string to the proper size to get all the data?  If not...
4) Did you close the file when finished with it?  If not...

All little things that we can code and work our way around with a little simple error checking...

OR....

Code: (Select All)
temp$ = _ReadFile$("temp.txt")


We simply use the _ReadFile$ function to read the file into any given string, and let the function handle existence checks, sizing, opening handles, closing handles, and all that good stuff.

_READFILE$ -- it really is the simplest way to read a file into a string!!  And even better....  IT REALLY IS THAT SIMPLE TO USE!!!  Big Grin
Reply
#2
Don't you ever sleep ???   Big Grin

But wow! That's a cool tool!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#3
(05-18-2024, 06:17 AM)SMcNeill Wrote: ....
We simply use the _ReadFile$ function to read the file into any given string, and let the function handle existence checks, sizing, opening handles, closing handles, and all that good stuff.
....

It will NOT do any existence checks !!
It also throws the usual "File not found" error. However, you may continue from that error and the function will not further complain about it, but simply returning an empty string.

Now as a file may exist, but still being empty, you could also regularly get back an empty string.
The only way to clarify why the result was empty is a _FILEEXIST call. And it's still the best to do this before calling _READFILE to avoid such "File not found" errors in the first place.
Reply
#4
@RhoSigma That "File Not Found" *is* an internal existence check.  It tells you if the file isn't found, and then exits the process for you.  Let me see if I can illustrate a little better what I'm trying to highlight here:

Code: (Select All)
Open "temp.txt" For Output As #1: Close 'create a file
Kill "temp.txt" '                        so we can kill that file to make certain it doesn't exist, without errors

If _FileExists("temp.txt") Then Print "Somethings wrong!  We kilt it and it didn't die!"
Open "temp.txt" For Binary As #1
temp$ = Space$(LOF(1))
Get #1, 1, temp$
Close

If _FileExists("temp.txt") Then
    Print "Even though there was no temp.txt, there is now!  OPEN FOR BINARY made one!"
Else
    Print "File still missing.  Open For Binary didn't make one."
End If

Kill "temp.txt" '              kill that file
If _FileExists("temp.txt") Then Print "Somethings wrong!  We kilt it and it didn't die!"

temp$ = _ReadFile$("temp.txt") ' and now readfile$ will toss an error for "File Not Found"

If _FileExists("temp.txt") Then
    Print "Even though there was no temp.txt, there is now!  ReadFile$ made one!"
Else
    Print "File still missing, as ReadFile didn't make one."
End If

The old method would automatically CREATE a file when you OPEN FOR BINARY, if none existed before.  That file would be size 0 in length; you'd read a 0-byte string, and then once you close the file, you're left with a size-0 file on your drive which you'd probably need to clean up.

With _ReadFile$, it checks to see if the file exists, and if it doesn't find it where you specified, it tosses the "File Not Found" error and then exits, returning you a 0-byte string if you click "Continue".  What it doesn't do is create that file on your drive anywhere, so you never have to worry about cleaning up any junk files from your drive.  Wink

That "File Not Found" *is* a form of an existence check...  at least, that's what I'd consider it to be.  I suppose someone else might consider it to be an ERROR.  I guess it's all just a matter of perspective on how you consider things.  Smile

What our new little command does, is actually something very similar to all the below here:

Code: (Select All)
Dim f As Long
Dim temp$

If _FileExists("temp.txt") = 0 Then
    _MessageBox "ERROR -- File Not Found", "Error!  File Not Found", "error"
Else
    f = FreeFile
    Open "temp.txt" For Binary As #f
    temp$ = Space$(LOF(f))
    Get #f, 1, temp$
    Close #f
End If

That's a pretty good reduction for a good bit of typing/coding, for one quick shortcut of a command.  Smile
Reply
#5
Ah see, the old method would simply create a size 0 file and return empty, and afterwards you couldn't even check if the file maybe not exists, as it always does exist after the OPEN call.

Well, if you take it that way, then _READFILE does exist checks, although I wouldn't take a error messagebox as a desireable exist check result nevertheless.

So my advise remains the same, always use _FILEEXIST first, if you can't guarantee the file is always there.
Reply




Users browsing this thread: 4 Guest(s)