DAY 037: FREEFILE
#1
We interrupt your regularly scheduled program for this brief commercial message!

Tired of spending your hard earned money on an overly-priced file? Well now you can avoid those high fees with this tried and true QB Keyword of the Day, FREEFILE!

SYNTAX filenum& = FREEFILE

Usage: Retrieves the next available open file handle.

So what's the benefit?

Well, for most small apps, we just code...

Code: (Select All)
OPEN "myfile.dat" FOR INPUT AS #1
'De Foo Foo Foo De blah, blah, blah...
CLOSE #1

However, let's say we make a big app, that uses 30 files, and some of them, depending on operations being called, stay opened!

Hmm, now keeping track of all those file numbers would require considerable thought. In fact, the only way to not to get into trouble, by using a file number which is still opened, would be to assign a unique number to each file operation, or... do it the easy way by letting the system figure it out for us. Well, that's exactly what FREEFILE does!

But first, please note: Even though we can assign a variable to FREEFILE before we OPEN the file, "You actually have to Open the file to trigger a value." Thanks, Dimster. So
please make sure you use good coding practice and associate each FREEFILE statement with each corresponding OPEN statement.

Code: (Select All)
DEFLNG f
ON Rob GOSUB ThePolice

ThePolice:
filenum1 = FREEFILE
OPEN "myfile-abc.dat" FOR INPUT AS filenum1
'De Foo Foo Foo De blah, blah, blah...

filenum2 = FREEFILE
OPEN "myfile-def.dat" FOR INPUT AS filenum2
'Is all I will output to you...

IF mydata$ = "EOF" THEN CLOSE #filenum1

filenum3 = FREEFILE
OPEN "myfile-ghi.dat" FOR INPUT AS filenum3
'De Foo Foo Foo De blah, blah, blah...

filenum4 = FREEFILE
OPEN "myfile-jkl.dat" FOR INPUT AS filenum4
' And now this FREEFILE demo's through.
CLOSE #filenum1, #filenum2, #filenum3, #filenum4
RETURN

So in the code example above, FREEFILE will assign the file handles either as 1, 2, 3, and 4 or, if mydata$ = "EOF" and the first file gets closed... 1, 2, 1, 3. Remember, FREEFILE is tracking your file use, and depending on conditional statements, like in our above example, your file numbering results will vary. Also, always remember to use good coding practices. Don't have a list of pre-assigned FREEFILE assignments, at the top of your program, without each corresponding file actually being opened. You will get duplicate file handles.

Oh, and if you forget to add the # symbol, don't worry; it's optional. I just like using it as a search marker. What;s that? You don't need no stinkin' search marker. Alrighty then, save yourself some typing by omitting it. Like I always say to my wife, "It takes a big man to omit when he's wrong!" Okay Steve, enough with the "big man" jokes, already...

And now back to your regularly scheduled program, DEEP Thoughts, by jack handy...

Do woke coders OPEN files as non-BINARY?

Pete
Reply


Messages In This Thread
DAY 037: FREEFILE - by Pete - 12-17-2022, 12:30 PM
RE: DAY 037: FREEFILE - by mnrvovrfc - 12-17-2022, 01:21 PM
RE: DAY 037: FREEFILE - by Dimster - 12-17-2022, 07:15 PM
RE: DAY 037: FREEFILE - by mnrvovrfc - 12-17-2022, 11:54 PM
RE: DAY 037: FREEFILE - by SMcNeill - 12-18-2022, 12:08 AM
RE: DAY 037: FREEFILE - by Dimster - 12-18-2022, 03:58 PM
RE: DAY 037: FREEFILE - by Pete - 12-18-2022, 04:30 PM
RE: DAY 037: FREEFILE - by Dimster - 12-18-2022, 05:08 PM
RE: DAY 037: FREEFILE - by Pete - 12-18-2022, 05:20 PM



Users browsing this thread: 3 Guest(s)