Extended KotD #8: _READFILE$ - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Official Links (https://qb64phoenix.com/forum/forumdisplay.php?fid=16) +--- Forum: Learning Resources and Archives (https://qb64phoenix.com/forum/forumdisplay.php?fid=13) +---- Forum: Keyword of the Day! (https://qb64phoenix.com/forum/forumdisplay.php?fid=49) +---- Thread: Extended KotD #8: _READFILE$ (/showthread.php?tid=2699) |
Extended KotD #8: _READFILE$ - SMcNeill - 05-18-2024 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)
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)
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!!! RE: Extended Kotd #8: _ReadFile$ - PhilOfPerth - 05-18-2024 Don't you ever sleep ??? But wow! That's a cool tool! RE: Extended Kotd #8: _ReadFile$ - RhoSigma - 05-18-2024 (05-18-2024, 06:17 AM)SMcNeill Wrote: .... 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. RE: Extended Kotd #8: _ReadFile$ - SMcNeill - 05-18-2024 @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)
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. 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. What our new little command does, is actually something very similar to all the below here: Code: (Select All)
That's a pretty good reduction for a good bit of typing/coding, for one quick shortcut of a command. RE: Extended Kotd #8: _ReadFile$ - RhoSigma - 05-18-2024 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. |