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


Messages In This Thread
Extended KotD #8: _READFILE$ - by SMcNeill - 05-18-2024, 06:17 AM
RE: Extended Kotd #8: _ReadFile$ - by PhilOfPerth - 05-18-2024, 07:24 AM
RE: Extended Kotd #8: _ReadFile$ - by RhoSigma - 05-18-2024, 09:06 AM
RE: Extended Kotd #8: _ReadFile$ - by SMcNeill - 05-18-2024, 09:42 AM
RE: Extended Kotd #8: _ReadFile$ - by RhoSigma - 05-18-2024, 11:08 AM



Users browsing this thread: 1 Guest(s)