Results 1 to 19 of 19

Thread: Whats the difference between FreeFile and...

  1. #1

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Whats the difference between FreeFile and...

    Whats the difference between open a file this way:

    VB Code:
    1. Dim fileChk, fileNum As Integer
    2.    
    3.     fileChk = GetAttr(strPath)
    4.    
    5.         If Not fileChk <> 0 Then
    6.             fileOpen = ""
    7.         Else
    8.             fileNum = FreeFile
    9.                 Open strPath For Input As #fileNum
    10.                     Line Input #fileNum, fileOpen
    11.                 Close #fileNum

    ...and opening it this way:

    VB Code:
    1. Dim fileChk As Integer
    2.    
    3.     fileChk = GetAttr(strPath)
    4.    
    5.         If Not fileChk <> 0 Then
    6.             fileOpen = ""
    7.         Else
    8.                 Open strPath For Input As #1
    9.                     Line Input #1, fileOpen
    10.                 Close #1

    ?

    I've seen people do it both ways, but I don't know the difference or which one is better/more practical.

  2. #2

  3. #3
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Whats the difference between FreeFile and...

    Quote Originally Posted by BrendanDavis
    Whats the difference between open a file this way:

    VB Code:
    1. Dim fileChk, fileNum As Integer
    2.    
    3.     fileChk = GetAttr(strPath)
    4.    
    5.         If Not fileChk <> 0 Then
    6.             fileOpen = ""
    7.         Else
    8.             fileNum = FreeFile
    9.                 Open strPath For Input As #fileNum
    10.                     Line Input #fileNum, fileOpen
    11.                 Close #fileNum

    ...and opening it this way:

    VB Code:
    1. Dim fileChk As Integer
    2.    
    3.     fileChk = GetAttr(strPath)
    4.    
    5.         If Not fileChk <> 0 Then
    6.             fileOpen = ""
    7.         Else
    8.                 Open strPath For Input As #1
    9.                     Line Input #1, fileOpen
    10.                 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.

  4. #4
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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 :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  5. #5
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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 :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  6. #6

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    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

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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...

  8. #8
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Whats the difference between FreeFile and...

    Quote Originally Posted by RhinoBull
    File Number is relevant within one program...
    Oops, didn't know that.

  9. #9
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    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.

  10. #10
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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?
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  11. #11

  12. #12
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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? Each app can open 512 files ... You may (and probably will) get sharing violations but that is entirely different story.

  13. #13
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    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!!

  14. #14
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  15. #15
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    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:
    1. Dim FileNum As Integer
    2.  
    3. FileNum = FreeFile()
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  16. #16
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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 :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  17. #17
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Whats the difference between FreeFile and...

    It's not wrong, it's just not needed. Long can still hold 1 or 512

  18. #18
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Whats the difference between FreeFile and...

    From MSDN:
    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.

  19. #19
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Whats the difference between FreeFile and...

    Thank you schoolbusdriver thats where I've read it.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width