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:
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....
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!!!
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!!!