Results 1 to 10 of 10

Thread: problem with binary fiile read and write

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    problem with binary fiile read and write

    i have this code to write to a binary file.
    VB Code:
    1. Private Sub doSave(saveFile As String)
    2.     Dim NewPos As Long
    3.    
    4.     Open saveFile For Binary As #1
    5.     Put #1, 1, "[Destination Folder] = " & SaveLoc & Chr(0)
    6.     NewPos = 1 + Len("[Destination Folder] = " & SaveLoc & Chr(0))
    7.    
    8.     Put #1, NewPos, "[Output Format] = " & SaveType & Chr(0)
    9.     NewPos = NewPos + Len("[Output Format] = " & SaveType & Chr(0))
    10.    
    11.     Put #1, NewPos, "[Extension Used] = " & SaveExt & Chr(0)
    12.     NewPos = NewPos + Len("[Extension Used] = " & SaveExt & Chr(0))
    13.    
    14.     Put #1, NewPos, "[Number of Files] = " & LstFiles.ListCount & Chr(0)
    15.     NewPos = NewPos + Len("[Number of Files] = " & LstFiles.ListCount & Chr(0))
    16.    
    17.     If ChkDelSource.Value = 0 Then
    18.         Put #1, NewPos, "[Delete Source] = False" & Chr(0)
    19.         NewPos = NewPos + Len("[Delete Source] = False" & Chr(0))
    20.     Else
    21.         Put #1, NewPos, "[Delete Source] = True" & Chr(0)
    22.         NewPos = NewPos + Len("[Delete Source] = True" & Chr(0))
    23.     End If
    24.    
    25.     Put #1, NewPos, "[List of Files] = " & Chr(0)
    26.     NewPos = NewPos + Len("[List of Files] = " & Chr(0))
    27.    
    28.     For tmpCnt = 0 To LstFiles.ListCount - 1
    29.         Put #1, NewPos, LstFiles.List(tmpCnt) & Chr(0)
    30.         NewPos = NewPos + Len(LstFiles.List(tmpCnt) & Chr(0))
    31.     Next tmpCnt
    32.     Close #1
    33. End Sub

    and i read from the file in this way
    VB Code:
    1. Open bsFile For Binary As #FF
    2.     cnt = 1
    3.     aCnt = 1
    4.     ReDim Preserve tLine(aCnt) As String
    5.     While Not EOF(1)
    6.         Get #FF, cnt, SomeChar
    7.         If SomeChar = 0 Then
    8.             aCnt = aCnt + 1
    9.             ReDim Preserve tLine(aCnt) As String
    10.         Else
    11.             tLine(aCnt) = tLine(aCnt) & Chr(SomeChar)
    12.         End If
    13.         cnt = cnt + 1
    14.     Wend
    15.     Close #FF

    and this is the result i get.
    
    
    [Destination Folder]
    
    [Output Format]
    
    [Extension Used]
    
    [Number of Files] =
    
    [Delete Source] = Fa
    
    [List of Files]
    L
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\add.
    M
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\base.
    P
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\caption.
    N
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\close.
    P
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\convert.
    T
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\destination.
    M
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\file.
    M
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\logo.
    Q
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\minimize.
    O
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\Option.
    O
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\output.
    M
    C:\Documents and Settings\Administrator\My Documents\My Pictures\bc\save.bmp
    what mistake am i making?

    thanx for u're help.

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: problem with binary fiile read and write

    mebhas,

    Why are you reading a text file as binary. Don't do that unless it is needed. Read the file as text.
    VB Code:
    1. Dim FNum as Integer
    2.  
    3. Fnum = FreeFile
    4. Open Filename for Input as #FNum

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: problem with binary fiile read and write

    i have successfully implemented the system of reading and writing the data to and from a text file(input and output) for the same purpose. what i do want to do now is to use the same data in binary format. isn't that how i'm supposed to do it? works for most of the data....

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: problem with binary fiile read and write

    mebhas,

    isn't that how i'm supposed to do it?
    No. As I stated not unless you really need to. There is no reason to read a text file as binary unless you are doing special processing.

    If you are going to process data as binary you should be familiar of what binary data actually looks like. What you have in your strings is carriage return line feed characters (vbCRLF
    ).

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: problem with binary fiile read and write

    so are those boxes full of vbcrlfs? now i get it. i think i should be checking to see what gets written in the file. in case u didn't notice, the first code is to write in binary file. the second code is to read from it.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: problem with binary fiile read and write

    Actually I think those characters are Chr(0), as added by your doSave sub.

    You could use the FileText sub from the FAQ forum (here)., then split the file by Chr(0) to get individual lines, eg:
    VB Code:
    1. Dim aFile as Variant  '(a variant is required for split :()
    2. aFile = Split(FileText(bsFile),Chr(0))

    However the need for binary files in this case is dubious, note tho that the Chr(0) will cause issues for standard file reading.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: problem with binary fiile read and write

    frankly speaking, i dont need binary files. the reason i want to do it tho is to practise it. havent used binary file reading and writing for some time now. and now that is am using it i get problems, and i dont know whats causing it.

  8. #8
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: problem with binary fiile read and write

    Look at your file in a hex editor and you will see what we are talking about. Those characters are Chr(0) and not CR/LF as si stated.

  9. #9
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: problem with binary fiile read and write

    Quote Originally Posted by mebhas
    frankly speaking, i dont need binary files. the reason i want to do it tho is to practise it. havent used binary file reading and writing for some time now. and now that is am using it i get problems, and i dont know whats causing it.
    Binary files are designed to store binary data (which can also include text). Data written to (and read from) binary files is usually designed to be interpreted by a custom application such as MSWord (.doc files) or Photoshop (.psd files) etc, so unless you're planning on creating your own file format you're best to stick with text files to store text.

    Chr(0) is basically the equivalent of NULL and really has no place in a text file, but is quite valid in a binary file.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  10. #10

    Re: problem with binary fiile read and write

    To be able to read in the way you are using then:
    VB Code:
    1. Private Sub SaveFile()
    2. Dim FileData() As String, Ind() As Long, FB As Integer
    3. ReDim FileData(1 To 10) ' Or whatever you want
    4. ReDim Ind(0)
    5. Ind(0) = 10
    6. FB = FreeFile
    7. Open "C:\file.txt" For Binary Lock Write As #FB
    8. Put #FB, 1, Ind()
    9. Put #FB, , FileData()
    10. Close #FB
    11. End Sub
    12.  
    13. Private Sub OpenFile()
    14. Dim FileData() As String, Ind() As Long, FB As Integer
    15. ReDim Ind(0)
    16. FB = FreeFile
    17. Open "C:\file.txt" For Binary Lock Read As #FB
    18. Get #FB, 1, Ind()
    19. ReDim FileData(1 To Ind(0))
    20. Get #FB, , FileData()
    21. Close #FB
    22. 'Whatever you want to do with the code
    23. End Sub

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