FILELEN() Function slow to update?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Elkin

    FILELEN() Function slow to update?

    I'm having a strange problem using random file access and was wondering if
    anyone is aware of why this might be happening. When I write new records to
    the file, it seems to take a few seconds before the FILELEN() function to
    update with the accurate length. As a test I did the following:

    Sub test()
    Dim inta As Integer

    For inta = 1 To 5

    Raffle.TicketsP urchased = inta
    Put #20, inta, Raffle
    Get #20, inta, Raffle
    'Delay (2) Notice this is commented out)
    intNumRows = FileLen(App.Pat h & "\herosraffle.d at") / Len(Raffle)
    Say (Str(intNumRows ) + " " + Str(Raffle.Tick etsPurchased))
    Next inta
    End Sub


    This gave me the following results

    1 1
    1 2
    2 3
    3 4
    4 5


    If I re-run the code with the 2 second delay added in I get

    1 1
    2 2
    3 3
    4 4
    5 5


    Why does it take so long for FILELEN() to update? Is there another way I
    can check for how many records are in the file using a different command?
    Adding a delay slows down the process this program is performing. Thanks in
    advance,
    -Keith


  • Steve Gerrard

    #2
    Re: FILELEN() Function slow to update?


    "Keith Elkin" <keith@keithelk in.com> wrote in message
    news:d%AQb.1452 3$O22.8858790@n ews4.srv.hcvlny .cv.net...[color=blue]
    > I'm having a strange problem using random file access and was[/color]
    wondering if[color=blue]
    > anyone is aware of why this might be happening. When I write new[/color]
    records to[color=blue]
    > the file, it seems to take a few seconds before the FILELEN() function[/color]
    to[color=blue]
    > update with the accurate length. As a test I did the following:
    >
    > Sub test()
    > Dim inta As Integer
    >
    > For inta = 1 To 5
    >
    > Raffle.TicketsP urchased = inta
    > Put #20, inta, Raffle
    > Get #20, inta, Raffle
    > 'Delay (2) Notice this is commented out)
    > intNumRows = FileLen(App.Pat h & "\herosraffle.d at") / Len(Raffle)
    > Say (Str(intNumRows ) + " " + Str(Raffle.Tick etsPurchased))
    > Next inta
    > End Sub
    >
    >
    > This gave me the following results
    >
    > 1 1
    > 1 2
    > 2 3
    > 3 4
    > 4 5
    >
    >
    > If I re-run the code with the 2 second delay added in I get
    >
    > 1 1
    > 2 2
    > 3 3
    > 4 4
    > 5 5
    >
    >
    > Why does it take so long for FILELEN() to update? Is there another[/color]
    way I[color=blue]
    > can check for how many records are in the file using a different[/color]
    command?[color=blue]
    > Adding a delay slows down the process this program is performing.[/color]
    Thanks in[color=blue]
    > advance,
    > -Keith
    >
    >[/color]
    Use LOF(#) to get the length of an open file (in your case, LOF(20).
    FileLen(Path) is for closed files. You also might want to use FreeFile
    to get a file number, instead of using a hard coded value.


    Comment

    • Randy Birch

      #3
      Re: FILELEN() Function slow to update?

      Data written to disk is a buffered action, which Windows does when it can.

      BTW, the proper formula for calculating the number of records in a random
      access file, once it has been opened, is:

      totalrecords = LOF(#hfile) \ Len(YourUDT)

      Notice too that the division is Integer division, not Floating Point, as
      there will never be a decimal.

      And conversely, FYI, the next new record for a random access file is always
      totalrecords + 1


      --

      Randy Birch
      MVP Visual Basic

      Please respond only to the newsgroups so all can benefit.

      There's no place like 127.0.0.1


      "Keith Elkin" <keith@keithelk in.com> wrote in message
      news:d%AQb.1452 3$O22.8858790@n ews4.srv.hcvlny .cv.net...
      : I'm having a strange problem using random file access and was wondering if
      : anyone is aware of why this might be happening. When I write new records
      to
      : the file, it seems to take a few seconds before the FILELEN() function to
      : update with the accurate length. As a test I did the following:
      :
      : Sub test()
      : Dim inta As Integer
      :
      : For inta = 1 To 5
      :
      : Raffle.TicketsP urchased = inta
      : Put #20, inta, Raffle
      : Get #20, inta, Raffle
      : 'Delay (2) Notice this is commented out)
      : intNumRows = FileLen(App.Pat h & "\herosraffle.d at") / Len(Raffle)
      : Say (Str(intNumRows ) + " " + Str(Raffle.Tick etsPurchased))
      : Next inta
      : End Sub
      :
      :
      : This gave me the following results
      :
      : 1 1
      : 1 2
      : 2 3
      : 3 4
      : 4 5
      :
      :
      : If I re-run the code with the 2 second delay added in I get
      :
      : 1 1
      : 2 2
      : 3 3
      : 4 4
      : 5 5
      :
      :
      : Why does it take so long for FILELEN() to update? Is there another way I
      : can check for how many records are in the file using a different command?
      : Adding a delay slows down the process this program is performing. Thanks
      in
      : advance,
      : -Keith
      :
      :


      Comment

      Working...