Whats the difference between FreeFile and...
Whats the difference between open a file this way:
VB Code:
Dim fileChk, fileNum As Integer
fileChk = GetAttr(strPath)
If Not fileChk <> 0 Then
fileOpen = ""
Else
fileNum = FreeFile
Open strPath For Input As #fileNum
Line Input #fileNum, fileOpen
Close #fileNum
...and opening it this way:
VB Code:
Dim fileChk As Integer
fileChk = GetAttr(strPath)
If Not fileChk <> 0 Then
fileOpen = ""
Else
Open strPath For Input As #1
Line Input #1, fileOpen
Close #1
?
I've seen people do it both ways, but I don't know the difference or which one is better/more practical.
Re: Whats the difference between FreeFile and...
There is no difference what so ever IF you do not open another file until curent is closed.
So, using FreeFile is safer if you have to work with multiple files at the same time.
Re: Whats the difference between FreeFile and...
Quote:
Originally Posted by BrendanDavis
Whats the difference between open a file this way:
VB Code:
Dim fileChk, fileNum As Integer
fileChk = GetAttr(strPath)
If Not fileChk <> 0 Then
fileOpen = ""
Else
fileNum = FreeFile
Open strPath For Input As #fileNum
Line Input #fileNum, fileOpen
Close #fileNum
...and opening it this way:
VB Code:
Dim fileChk As Integer
fileChk = GetAttr(strPath)
If Not fileChk <> 0 Then
fileOpen = ""
Else
Open strPath For Input As #1
Line Input #1, fileOpen
Close #1
?
I've seen people do it both ways, but I don't know the difference or which one is better/more practical.
FreeFile is better.
FreeFile gets an available file handle number by Windows to use when opening a file. It will always work.
#1 just uses "1" as a file handle number.
File handles are usually long numbers and the chances of it returning "1" or "2" are slim to none.
But if there's another VB program running, and the programmer also decided to use "1" as the file handle number, then you would get an error.
In short, use FreeFile. :)
Re: Whats the difference between FreeFile and...
if you use freefile, you stop the chance of a program crash from happening because of you trying to use the same number twice....notice how you have #1 in the second way, if you then tried opening ANOTHER file using the same number you'd get an error...try it and see :-)
Re: Whats the difference between FreeFile and...
Quote:
Originally Posted by DigiRev
But if there's another VB program running, and the programmer also decided to use "1" as the file handle number, then you would get an error.
I'm not so sure about that, actually...the numbers themselves are usually program-specific...you could have 1000 programs all opening using #1, and problems only occur if one program tries to open twice using the same number without closing first :-)
Re: Whats the difference between FreeFile and...
Thanks for the input (pun intended). I thought it was something along those lines, but just to be sure... I had to ask :)
Re: Whats the difference between FreeFile and...
Quote:
Originally Posted by DigiRev
...But if there's another VB program running, and the programmer also decided to use "1" as the file handle number, then you would get an error...
File Number is relevant within one program... :)
Re: Whats the difference between FreeFile and...
Quote:
Originally Posted by RhinoBull
File Number is relevant within one program... :)
Oops, didn't know that. :blush:
Re: Whats the difference between FreeFile and...
Quote:
Originally Posted by RhinoBull
File Number is relevant within one program... :)
That's quite logical. I think that #512 is max. However, if you'd have 512 applications running (theoretically), you could open only 1 file per app. And it just cannot be so.
Re: Whats the difference between FreeFile and...
Quote:
Originally Posted by RhinoBull
File Number is relevant within one program... :)
But if you tried opening #1 twice in one program (which is what I was talking about) it would crash...as I said...I at no point stated more than one program, did I?
Re: Whats the difference between FreeFile and...
I think I quoted the wrong reply so relax smUX.
Re: Whats the difference between FreeFile and...
Quote:
Originally Posted by gavio
That's quite logical. I think that #512 is max. However, if you'd have 512 applications running (theoretically), you could open only 1 file per app. And it just cannot be so.
What? :confused: Each app can open 512 files ... You may (and probably will) get sharing violations but that is entirely different story.
Re: Whats the difference between FreeFile and...
Quote:
Originally Posted by me
That's quite logical. I think that #512 is max. However, if you'd have 512 applications running (theoretically), you could open only 1 file per app. And it just cannot be so.
Uhhh!! :rolleyes:
Re: Whats the difference between FreeFile and...
Quote:
Originally Posted by RhinoBull
I think I quoted the wrong reply so relax smUX.
You just saw my name and thought you'd jump on my reply, as usual..."Oh, it's SmUX, he must be wrong"...logic fails you yet again :-P
Re: Whats the difference between FreeFile and...
Quote:
Originally Posted by DigiRev
File handles are usually long numbers
File numbers/handles are Integer. ;)
VB Code:
Dim FileNum As Integer
FileNum = FreeFile()
Re: Whats the difference between FreeFile and...
Quote:
Originally Posted by Keithuk
File numbers/handles are Integer. ;)
I think he meant long numbers as in length, although even then it'd be wrong if the max file number is 512 :-)
Re: Whats the difference between FreeFile and...
It's not wrong, it's just not needed. Long can still hold 1 or 512 :)
Re: Whats the difference between FreeFile and...
From MSDN: :p
Quote:
FreeFile Function
Description
Returns an Integer representing the next file number available for use by the Open statement.
Syntax
FreeFile[(rangenumber)]
The optional rangenumber argument is a Variant that specifies the range from which the next free file number is to be returned. Specify a 0 (default) to return a file number in the range 1 – 255, inclusive. Specify a 1 to return a file number in the range 256 – 511.
Re: Whats the difference between FreeFile and...
Thank you schoolbusdriver thats where I've read it. ;)