Results 1 to 3 of 3
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Expanding BINARY file length (all)

    <pre>Option Explicit
    Function test()

    ' This vehicle demonstrates a facet of writing binray to a text file.
    ' The file is seen to grow to the length of the longest string ever written to it.

    Dim intFile As Integer
    intFile = FreeFile
    ' Test assumes clean environment, no other files open,_
    ' no file left open from a previous test.
    If intFile <> 1 Then MsgBox "some error 1"
    ' If you don't have a C:temp this will cause an error.
    Open "c:temperaseme.txt" For Binary As intFile
    Dim strBuffer As String

    ' first write
    ' Assumes the file did not exist prior to this test.
    ' (See the KILL at the end of the procedure)

    ' Prepare a data record 10 characters in length, write it, _
    ' report the length of the file.
    strBuffer = "0123456789"
    Put #intFile, 1, strBuffer
    MsgBox LOF(intFile) ' should return "10"; returns "11" if you didn't KILL the file.

    ' second write
    ' Assumes the file exists.

    ' Prepare a data record 11 characters in length, write it, _
    ' report the length of the file.
    strBuffer = "01234567890"
    Put #intFile, 1, strBuffer
    MsgBox LOF(intFile) ' should return "11"

    ' third write
    ' Assumes the file exists.

    ' Prepare a data record 4 characters in length, _
    ' write it to the middle of the file, report the length of the file.
    strBuffer = "0123"
    Put #intFile, 3, strBuffer
    MsgBox LOF(intFile) ' should return "11", because we have not pushed past the previous length of the file.

    ' Now display the string of the entire file.
    ' Note the embedded "0123"
    Dim strFileString As String
    strFileString = String$(LOF(intFile), " ") ' create buffer for GET routine
    Get #intFile, 1, strFileString
    MsgBox strFileString

    Close intFile

    ' Now demonstrate that by KILLing the file and creating a new file, _
    ' the length can be reduced
    Kill "c:temperaseme.txt"

    intFile = FreeFile
    If intFile <> 1 Then MsgBox "some error 1"
    Open "c:temperaseme.txt" For Binary As intFile
    ' fourth write
    ' Assumes the file did not exist prior to this test.
    ' (See the KILL at the end of the procedure)

    ' Prepare a data record 10 characters in length, _
    ' write it, report the length of the file.
    strBuffer = "0123456789"
    Put #intFile, 1, strBuffer
    MsgBox LOF(intFile) ' should return "10"; returns "11" if you didn't KILL the file.

    ' Now display the string of the entire file.
    strFileString = String$(LOF(intFile), " ") ' create buffer for GET routine
    Get #intFile, 1, strFileString
    MsgBox strFileString

    Close intFile

    Kill "c:temperaseme.txt"

    End Function
    </pre>


  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Expanding BINARY file length (all)

    What are we demonstrating here, Chris?

    If you are getting an extra "character" on your writes to a text file is it because text files have an end of file character (I believe it's ascii 12)?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Expanding BINARY file length (all)

    We are demonstrating that the length of a binary file will be the maximum of the original length and the length of the string being written. More correctly, the length of the file and the maximum of the sum of the position and length of the string being written.

    If you change the string in the second WRITE to be 15 characters in length, you'll get a length of 15 returned until the file is Killed.

    It's nothing to do with an extra character off the end of the file - I could deal with that pretty easily - it's to do with wanting to write a SAMALLER file than the original file. It's to do with reading a 22 character file and wanting to rewrite it as a shorter file.

    Such an event takes place when I want to replca eone string ("pdf=alpha") with a shorter string ("pdf=beta") as would happen in the management of an INI file using the binary write mode.

Posting Permissions

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