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
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
Code: Select all
'close #(freefile()) ----> crash
close ' closes all open Files (no comment, crash etc.)
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,jevans4949 wrote:Code: Select all
Dim xfileno As Integer xfileno = FreeFile Open "xfile.txt" For Output As #xfileno Print #xfileno, "Hello, World" Close #xfileno
Code: Select all
open "xfile" for output as #(freefile())
dim as long ff = FreeFile
close( #(ff-1) )
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.MrSwiss wrote:A hack:No more "later changing Rules", please!Code: Select all
open "xfile" for output as #(freefile()) dim as long ff = FreeFile close( #(ff-1) )
Code: Select all
close( (FreeFile()-1) )
This works indeed!MrSwiss wrote:It appears to me, that you're trying to be difficult, because it can also be done without a Variable.This closes the "last" opened File.Code: Select all
close( (FreeFile()-1) )
Code: Select all
open "x" for output as #(freefile())
dim as long lastUsedFF => FreeFile() - 1
'....
close #lastUsedFF
Code: Select all
open "x" for output as #(cInt(256*rnd()))
'close #?
This is exactly what I've been trying to "get across" the whole Day long!Doing so there is no fear to loose track of the file number if some new freefile is called before the file is closed.
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.MrSwiss wrote:This is exactly what I've been trying to "get across" the whole Day long!
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
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.Do someone see a way to close it in a quick way, avoiding crash? --- edit: without changing this dumb open method.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
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.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
Tourist Trap wrote:Yep. I would have prefered something like dom=handle("x") but remains probably very tricky.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
You are right, and I find that file management in FB is rather complicated in general.fxm wrote: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 ... as #freeFile()
dim as long ff = freeFile()-1
close #ff