How can I close this file (and only this one)?

General FreeBASIC programming questions.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

How can I close this file (and only this one)?

Post by Tourist Trap »

I have a file opened and created with Open() that I can not close. It's more for the challenge than a real life issue but it's a relatively funny issue.

Code: Select all

........  'any code (can't be changed)
........
open "xfile" for output as #(freefile())   'code can't be changed before this line of code

'close #(freefile())  ----> crash
Do someone see a way to close it in a quick way, avoiding crash? --- edit: without changing this dumb open method.
Last edited by Tourist Trap on Apr 06, 2016 17:24, edited 2 times in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How can I close this file?

Post by MrSwiss »

Code: Select all

'close #(freefile())  ----> crash
close    ' closes all open Files (no comment, crash etc.)
The problem is:
you are assigning #2 (while attempting to "close"), to a File opened as #1 ...
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: How can I close this file?

Post by jevans4949 »

Code: Select all

Dim xfileno As Integer
xfileno = FreeFile
Open "xfile.txt" For Output As #xfileno
Print #xfileno, "Hello, World"
Close #xfileno
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How can I close this file?

Post by Tourist Trap »

jevans4949 wrote:

Code: Select all

Dim xfileno As Integer
xfileno = FreeFile
Open "xfile.txt" For Output As #xfileno
Print #xfileno, "Hello, World"
Close #xfileno
Hi jevans4949, hi Mr.Swiss,

you failed here since the challenge is precisely to close the file when it has been opened without obvious record of the file handle. It can perfectly occure if you have a thing like #include "someverylongandtediousfile.bas". So the question is how to do then.

Ok, maybe usage of Close() alone can do the job but it's rather radical because it closes every files. Is there another way to close only this given file, or say the last opened file??
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How can I close this file (and only this one)?

Post by MrSwiss »

A hack:

Code: Select all

open "xfile" for output as #(freefile())
dim as long ff = FreeFile
close( #(ff-1) )
No more "later changing Rules", please!
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How can I close this file (and only this one)?

Post by Tourist Trap »

MrSwiss wrote:A hack:

Code: Select all

open "xfile" for output as #(freefile())
dim as long ff = FreeFile
close( #(ff-1) )
No more "later changing Rules", please!
There is no changing rules. I probably should have been more precise about the fact that I wished the most general solution as possible, something that would work even from inside a program as complex as possible.
I know that freeFile() returns the more little unused value, but I could also have used a random number. It's just that with freeFile() it's more funny since this function is theorically designed to avoid any problem. But what is already a little difficult is that the freeFile() result must be saved in a variable outside the Open statement.

So maybe there is no solution, but who knows?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How can I close this file (and only this one)?

Post by MrSwiss »

It appears to me, that you're trying to be difficult, because it can also be done without a Variable.

Code: Select all

close( (FreeFile()-1) )
This closes the "last" opened File.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How can I close this file (and only this one)?

Post by Tourist Trap »

MrSwiss wrote:It appears to me, that you're trying to be difficult, because it can also be done without a Variable.

Code: Select all

close( (FreeFile()-1) )
This closes the "last" opened File.
This works indeed!
At least I would just do this differently:

Code: Select all

open "x" for output as #(freefile())
dim as long lastUsedFF => FreeFile() - 1
'....
close #lastUsedFF 
Doing so there is no fear to loose track of the file number if some new freefile is called before the file is closed.

Now this below is more complicated.

Code: Select all

open "x" for output as #(cInt(256*rnd()))
'close #?
Last edited by Tourist Trap on Apr 06, 2016 17:53, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: How can I close this file (and only this one)?

Post by MrSwiss »

Doing so there is no fear to loose track of the file number if some new freefile is called before the file is closed.
This is exactly what I've been trying to "get across" the whole Day long!
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How can I close this file (and only this one)?

Post by Tourist Trap »

MrSwiss wrote:This is exactly what I've been trying to "get across" the whole Day long!
I didn't say that "in general". If we are inside a function, then the local variable will possibly get lost at exiting while the handle is persistent. That's a remark I've made in the topic about the files functions, opened in documentation section. I still don't know the complete rule for file handle numbers (when are they destroyed exactly), but they seem clearly to have a longer lifetime than local variables.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How can I close this file (and only this one)?

Post by fxm »

This is much more complicated, because the same file can be open simultaneously multiple times in input and once in output:

Code: Select all

Open "test.txt" for output As #1
  Print #1, "FreeBASIC"
  Print #1, "1.06.0"
Close #1

Open "test.txt" for input As #1
  Dim As String s1
  Line Input #1, s1
  Open "test.txt" for input As #2
    Dim As String s2
    Line Input #2, s2
    Print s2
    Line Input #2, s2
    Print s2
    Print
    Open "test.txt" for output As #3
    Print #3, s1 & " " & s2
    Close #3
  Close #2  
Close #1

Open "test.txt" for input As #1
  Dim As String s
  Line Input #1, s
  Print s
Close #1

Sleep
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Re: How can I close this file (and only this one)?

Post by integer »

Tourist Trap wrote:I have a file opened and created with Open() that I can not close. It's more for the challenge than a real life issue but it's a relatively funny issue.

Code: Select all

........  'any code (can't be changed)
........
open "xfile" for output as #(freefile())   'code can't be changed before this line of code

'close #(freefile())  ----> crash
Do someone see a way to close it in a quick way, avoiding crash? --- edit: without changing this dumb open method.

immediately following the open statement, get the file handle.

Code: Select all

...
open "xfile" for output as #(freefile())   'code can't be changed before this line of code
dim as integer dom = freefile -1
...
close #dom
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How can I close this file (and only this one)?

Post by Tourist Trap »

integer wrote: immediately following the open statement, get the file handle.

Code: Select all

...
open "xfile" for output as #(freefile())   'code can't be changed before this line of code
dim as integer dom = freefile -1
...
close #dom
Yep. I would have prefered something like dom=handle("x") but remains probably very tricky.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How can I close this file (and only this one)?

Post by Tourist Trap »

Tourist Trap wrote:
integer wrote: immediately following the open statement, get the file handle.

Code: Select all

...
open "xfile" for output as #(freefile())   'code can't be changed before this line of code
dim as integer dom = freefile -1
...
close #dom
Yep. I would have prefered something like dom=handle("x") but remains probably very tricky.
fxm wrote:This is much more complicated, because the same file can be open simultaneously multiple times in input and once in output
You are right, and I find that file management in FB is rather complicated in general.
noop
Posts: 130
Joined: Sep 18, 2006 10:29

Re: How can I close this file (and only this one)?

Post by noop »

Perhaps I misunderstood what was written above but the following seems dangerous:

Code: Select all

open ... as #freeFile()
dim as long ff = freeFile()-1
close #ff
As far as I'm aware, internally, there is an array of file handles. freeFile simply returns the smallest index into the array with an empty cell. So you could open 10 files in a row and get the file numbers 1 to 10.
Then close #5 and query freeFile. It should return 5. After that freeFile should return 11. Thus the code above wouldn't work.
Post Reply