FreeFile


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: FreeFile

  1. #1
    David Guest

    FreeFile


    Group:

    I have been trying to learn some of the commands and methods of VB and one

    shown was FreeFile which I understand gets the next available file number
    to store data.

    The following is part of suggested code to play with, but won't execute
    because it has a bad file name or number.

    Does that mean that the File Name which is a concatenation of a literal
    "Test" and Index number were not "Dim"med or perhaps the file number
    should havew been a pathway?

    Would appreciate any suggestions or comments

    David


    Private Sub Main()

    For MyIndex = 1 To 5 ' Loop 5 times
    FileNumber = FreeFile(0) ' Get unused file numbers

    Open "Test" & MyIndex For Output _
    As #FileNumber
    Write #FileNumber, "This is a Sample"
    Close #FileNumber
    Next MyIndex

    End Sub


  2. #2
    Rick Rothstein Guest

    Re: FreeFile

    Your code ran fine for me; no error messages.

    On a different note, I believe (but am not 100% sure) that the way you are using the
    FreeFile could lead to problems when writing large files. FreeFile is only guaranteed to
    return an unused file number; it does not guarantee not to reuse already used ones. If say
    file number 1 were to become fully available during the say loop number 4, I believe
    FreeFile could return 1 during the 4th loop instead of 4.

    This didn't happen during my test, but that is my understanding. Perhaps the speed at
    which VB closes files is slower than the speed required to open and write, 5 times, the
    very short text of your example. As I said, I'm not 100% per cent sure on this so perhaps
    someone can support or refute my understanding?

    Rick

    "David" <walterdw@hal-pc.org> wrote in message news:3b4c8a6f@news.devx.com...
    >
    > Group:
    >
    > I have been trying to learn some of the commands and methods of VB and one
    >
    > shown was FreeFile which I understand gets the next available file number
    > to store data.
    >
    > The following is part of suggested code to play with, but won't execute
    > because it has a bad file name or number.
    >
    > Does that mean that the File Name which is a concatenation of a literal
    > "Test" and Index number were not "Dim"med or perhaps the file number
    > should havew been a pathway?
    >
    > Would appreciate any suggestions or comments
    >
    > David
    >
    >
    > Private Sub Main()
    >
    > For MyIndex = 1 To 5 ' Loop 5 times
    > FileNumber = FreeFile(0) ' Get unused file numbers
    >
    > Open "Test" & MyIndex For Output _
    > As #FileNumber
    > Write #FileNumber, "This is a Sample"
    > Close #FileNumber
    > Next MyIndex
    >
    > End Sub
    >



  3. #3
    Arthur Wood Guest

    Re: FreeFile


    David,

    The call to FreeFile does not need an Argument:

    For MyIndex = 1 To 5 ' Loop 5 times
    FileNumber = FreeFile() ' Get unused file numbers

    Open "Test" & MyIndex For Output As #FileNumber
    Write #FileNumber, "This is a Sample"
    Close #FileNumber
    Next MyIndex

    In addition, you should ALWYS DECLARE all variables used in you code, using
    the DIM keyword:

    Dim MyIndex as Integer
    Dim FileNumber as Long

    For MyIndex = 1 To 5 ' Loop 5 times
    FileNumber = FreeFile() ' Get unused file numbers

    Open "Test" & MyIndex For Output As #FileNumber
    Write #FileNumber, "This is a Sample"
    Close #FileNumber
    Next MyIndex


    At the very top of the code module where this is found, you should also add
    the line :

    Option Explicit

    That will FORCE you to declare all varaiblesd BEFORE they are used.

    As the code you showed is written, the two variables that you use (MyIndex
    and FileNumber) are in fact created as type VARIANT.

    Arthur Wood
    "David" <walterdw@hal-pc.org> wrote:
    >
    >Group:
    >
    >I have been trying to learn some of the commands and methods of VB and one
    >
    >shown was FreeFile which I understand gets the next available file number
    >to store data.
    >
    >The following is part of suggested code to play with, but won't execute


    >because it has a bad file name or number.
    >
    >Does that mean that the File Name which is a concatenation of a literal
    >"Test" and Index number were not "Dim"med or perhaps the file number
    >should havew been a pathway?
    >
    >Would appreciate any suggestions or comments
    >
    >David
    >
    >
    >Private Sub Main()
    >
    > For MyIndex = 1 To 5 ' Loop 5 times
    > FileNumber = FreeFile(0) ' Get unused file numbers
    >
    > Open "Test" & MyIndex For Output _
    > As #FileNumber
    > Write #FileNumber, "This is a Sample"
    > Close #FileNumber
    > Next MyIndex
    >
    >End Sub
    >



  4. #4
    mrfelis Guest

    Re: FreeFile

    Rick Rothstein <rick_newsgroup@email.com> wrote in message
    news:3b4c90df@news.devx.com...
    > very short text of your example. As I said, I'm not 100% per cent
    > sure on this so perhaps
    > someone can support or refute my understanding?
    >

    I was under the same impression, but couldn't be entirely sure. So I wrote
    this really cheesy sample to prove one way or the other:

    Private Sub Command1_Click()
    Text1.Text = FreeFile
    Open Text1.Text For Output As #Text1.Text

    End Sub


    Private Sub Command2_Click()
    Close #Text1.Text
    End Sub


    FreeFile will return the lowest handle available. So if you open files #1 -
    5, then Close #2. FreeFile will return 2, then 6.

    Also note, successive calls to freefile will return the *same* number until
    you open or close another file. So if you are working with 2 or more files
    this wil cause an error:

    iFile1 = FreeFile
    iFile2 = FreeFile
    Open "MyFile1" .. As #iFile1
    Open "MyFile2" .. As #iFile2

    Instead, call free file just before opening the file. Like so:

    iFile1 = FreeFile
    Open "MyFile1" .. As #iFile1

    iFile2 = FreeFile
    Open "MyFile2" .. As #iFile2

    --
    ~~~
    !ti timda I ,KO
    ..em deppals nocaeB sivaM
    !draH
    ~~
    C'Ya,
    mrfelis@yahoo!com

    > Rick
    >
    > "David" <walterdw@hal-pc.org> wrote in message

    news:3b4c8a6f@news.devx.com...
    > >
    > > Group:
    > >
    > > I have been trying to learn some of the commands and methods of VB and

    one
    > >
    > > shown was FreeFile which I understand gets the next available file

    number
    > > to store data.
    > >
    > > The following is part of suggested code to play with, but won't execute
    > > because it has a bad file name or number.
    > >
    > > Does that mean that the File Name which is a concatenation of a literal
    > > "Test" and Index number were not "Dim"med or perhaps the file number
    > > should havew been a pathway?
    > >
    > > Would appreciate any suggestions or comments
    > >
    > > David
    > >
    > >
    > > Private Sub Main()
    > >
    > > For MyIndex = 1 To 5 ' Loop 5 times
    > > FileNumber = FreeFile(0) ' Get unused file

    numbers
    > >
    > > Open "Test" & MyIndex For Output _
    > > As #FileNumber
    > > Write #FileNumber, "This is a Sample"
    > > Close #FileNumber
    > > Next MyIndex
    > >
    > > End Sub
    > >

    >




  5. #5
    David Guest

    Re: FreeFile


    I'm not sure if this allows me to resppnd to an individaul replay, but
    I'll see.

    To Rick (and others) thanks for taking time. Good points all!, but I think
    I
    I have something amiss. I took and copied the code (without the recommended

    variable declarations and all) onto a different computer that used 95 as
    its operating system and a single harddirve, 3 partitions. The program ran
    and I could see the text in the files. But when I went back to the
    original computer which uses 2000 as the operating system and 3 harddrives
    installed to isolate operating systems and utilities, app programs & data
    a third disk to isolate net activity, the simple program wouldn't run, again\
    with the bad file name or number error message, pathway problem?



    "Rick Rothstein" <rick_newsgroup@email.com> wrote:
    >Your code ran fine for me; no error messages.
    >
    >On a different note, I believe (but am not 100% sure) that the way you are

    using the
    >FreeFile could lead to problems when writing large files. FreeFile is only

    guaranteed
    >to
    >return an unused file number; it does not guarantee not to reuse already

    used ones.
    >If say
    >file number 1 were to become fully available during the say loop number

    4, I believe
    >FreeFile could return 1 during the 4th loop instead of 4.
    >
    >This didn't happen during my test, but that is my understanding. Perhaps

    the speed at
    >which VB closes files is slower than the speed required to open and write,

    5 times,
    >the
    >very short text of your example. As I said, I'm not 100% per cent sure on

    this so perhaps
    >someone can support or refute my understanding?
    >
    >Rick
    >
    >"David" <walterdw@hal-pc.org> wrote in message news:3b4c8a6f@news.devx.com...
    >>
    >> Group:
    >>
    >> I have been trying to learn some of the commands and methods of VB and

    one
    >>
    >> shown was FreeFile which I understand gets the next available file number
    >> to store data.
    >>
    >> The following is part of suggested code to play with, but won't execute
    >> because it has a bad file name or number.
    >>
    >> Does that mean that the File Name which is a concatenation of a literal
    >> "Test" and Index number were not "Dim"med or perhaps the file number
    >> should havew been a pathway?
    >>
    >> Would appreciate any suggestions or comments
    >>
    >> David
    >>
    >>
    >> Private Sub Main()
    >>
    >> For MyIndex = 1 To 5 ' Loop 5 times
    >> FileNumber = FreeFile(0) ' Get unused file numbers
    >>
    >> Open "Test" & MyIndex For Output _
    >> As #FileNumber
    >> Write #FileNumber, "This is a Sample"
    >> Close #FileNumber
    >> Next MyIndex
    >>
    >> End Sub
    >>

    >



  6. #6
    Rick Rothstein Guest

    Re: FreeFile

    As a follow up .... my first test of your code was done on Win98SE using
    VB6SP3. I just tried it at my present location and your code ran fine here
    too on WinNT4SP6 also using VB6SP3.

    Try starting a new project and paste your code into it and run that to see
    if you still get the error.

    Rick


    "David" <walterdw@hal-pc.org> wrote in message
    news:3b4cbe8d$1@news.devx.com...
    >
    > I'm not sure if this allows me to resppnd to an individaul replay, but
    > I'll see.
    >
    > To Rick (and others) thanks for taking time. Good points all!, but I

    think
    > I
    > I have something amiss. I took and copied the code (without the

    recommended
    >
    > variable declarations and all) onto a different computer that used 95 as
    > its operating system and a single harddirve, 3 partitions. The program

    ran
    > and I could see the text in the files. But when I went back to the
    > original computer which uses 2000 as the operating system and 3 harddrives
    > installed to isolate operating systems and utilities, app programs & data
    > a third disk to isolate net activity, the simple program wouldn't run,

    again\
    > with the bad file name or number error message, pathway problem?
    >
    >
    >
    > "Rick Rothstein" <rick_newsgroup@email.com> wrote:
    > >Your code ran fine for me; no error messages.
    > >
    > >On a different note, I believe (but am not 100% sure) that the way you

    are
    > using the
    > >FreeFile could lead to problems when writing large files. FreeFile is

    only
    > guaranteed
    > >to
    > >return an unused file number; it does not guarantee not to reuse already

    > used ones.
    > >If say
    > >file number 1 were to become fully available during the say loop number

    > 4, I believe
    > >FreeFile could return 1 during the 4th loop instead of 4.
    > >
    > >This didn't happen during my test, but that is my understanding. Perhaps

    > the speed at
    > >which VB closes files is slower than the speed required to open and

    write,
    > 5 times,
    > >the
    > >very short text of your example. As I said, I'm not 100% per cent sure on

    > this so perhaps
    > >someone can support or refute my understanding?
    > >
    > >Rick
    > >
    > >"David" <walterdw@hal-pc.org> wrote in message

    news:3b4c8a6f@news.devx.com...
    > >>
    > >> Group:
    > >>
    > >> I have been trying to learn some of the commands and methods of VB and

    > one
    > >>
    > >> shown was FreeFile which I understand gets the next available file

    number
    > >> to store data.
    > >>
    > >> The following is part of suggested code to play with, but won't execute
    > >> because it has a bad file name or number.
    > >>
    > >> Does that mean that the File Name which is a concatenation of a literal
    > >> "Test" and Index number were not "Dim"med or perhaps the file number
    > >> should havew been a pathway?
    > >>
    > >> Would appreciate any suggestions or comments
    > >>
    > >> David
    > >>
    > >>
    > >> Private Sub Main()
    > >>
    > >> For MyIndex = 1 To 5 ' Loop 5 times
    > >> FileNumber = FreeFile(0) ' Get unused file

    numbers
    > >>
    > >> Open "Test" & MyIndex For Output _
    > >> As #FileNumber
    > >> Write #FileNumber, "This is a Sample"
    > >> Close #FileNumber
    > >> Next MyIndex
    > >>
    > >> End Sub
    > >>

    > >

    >




  7. #7
    Rick Rothstein Guest

    Re: FreeFile

    > FreeFile will return the lowest handle available. So if you open files #1 -
    > 5, then Close #2. FreeFile will return 2, then 6.


    Thanks for the test and confirmation.


    > Also note, successive calls to freefile will return the *same* number until
    > you open or close another file. So if you are working with 2 or more files
    > this wil cause an error:


    Which, of course, makes sense since it will return the lowest (thanks to your test above)
    currently available file number. Call it twice in a row without using it and it returns
    the same, lowest available file number each time (it you didn't use the number it returned
    the first time, that number is *still* available for the second function call).


    Rick


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center