|
-
Nov 6th, 2005, 04:23 AM
#1
Thread Starter
Frenzied Member
problem with binary fiile read and write
i have this code to write to a binary file.
VB Code:
Private Sub doSave(saveFile As String)
Dim NewPos As Long
Open saveFile For Binary As #1
Put #1, 1, "[Destination Folder] = " & SaveLoc & Chr(0)
NewPos = 1 + Len("[Destination Folder] = " & SaveLoc & Chr(0))
Put #1, NewPos, "[Output Format] = " & SaveType & Chr(0)
NewPos = NewPos + Len("[Output Format] = " & SaveType & Chr(0))
Put #1, NewPos, "[Extension Used] = " & SaveExt & Chr(0)
NewPos = NewPos + Len("[Extension Used] = " & SaveExt & Chr(0))
Put #1, NewPos, "[Number of Files] = " & LstFiles.ListCount & Chr(0)
NewPos = NewPos + Len("[Number of Files] = " & LstFiles.ListCount & Chr(0))
If ChkDelSource.Value = 0 Then
Put #1, NewPos, "[Delete Source] = False" & Chr(0)
NewPos = NewPos + Len("[Delete Source] = False" & Chr(0))
Else
Put #1, NewPos, "[Delete Source] = True" & Chr(0)
NewPos = NewPos + Len("[Delete Source] = True" & Chr(0))
End If
Put #1, NewPos, "[List of Files] = " & Chr(0)
NewPos = NewPos + Len("[List of Files] = " & Chr(0))
For tmpCnt = 0 To LstFiles.ListCount - 1
Put #1, NewPos, LstFiles.List(tmpCnt) & Chr(0)
NewPos = NewPos + Len(LstFiles.List(tmpCnt) & Chr(0))
Next tmpCnt
Close #1
End Sub
and i read from the file in this way
VB Code:
Open bsFile For Binary As #FF
cnt = 1
aCnt = 1
ReDim Preserve tLine(aCnt) As String
While Not EOF(1)
Get #FF, cnt, SomeChar
If SomeChar = 0 Then
aCnt = aCnt + 1
ReDim Preserve tLine(aCnt) As String
Else
tLine(aCnt) = tLine(aCnt) & Chr(SomeChar)
End If
cnt = cnt + 1
Wend
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.
-
Nov 6th, 2005, 04:36 AM
#2
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:
Dim FNum as Integer
Fnum = FreeFile
Open Filename for Input as #FNum
-
Nov 6th, 2005, 04:41 AM
#3
Thread Starter
Frenzied Member
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....
-
Nov 6th, 2005, 04:46 AM
#4
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
).
-
Nov 6th, 2005, 05:09 AM
#5
Thread Starter
Frenzied Member
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.
-
Nov 6th, 2005, 12:53 PM
#6
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:
Dim aFile as Variant '(a variant is required for split :()
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.
-
Nov 6th, 2005, 10:34 PM
#7
Thread Starter
Frenzied Member
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.
-
Nov 7th, 2005, 12:47 AM
#8
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.
-
Nov 7th, 2005, 01:46 AM
#9
Re: problem with binary fiile read and write
 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.
-
Nov 7th, 2005, 05:11 AM
#10
Member
Re: problem with binary fiile read and write
To be able to read in the way you are using then:
VB Code:
Private Sub SaveFile()
Dim FileData() As String, Ind() As Long, FB As Integer
ReDim FileData(1 To 10) ' Or whatever you want
ReDim Ind(0)
Ind(0) = 10
FB = FreeFile
Open "C:\file.txt" For Binary Lock Write As #FB
Put #FB, 1, Ind()
Put #FB, , FileData()
Close #FB
End Sub
Private Sub OpenFile()
Dim FileData() As String, Ind() As Long, FB As Integer
ReDim Ind(0)
FB = FreeFile
Open "C:\file.txt" For Binary Lock Read As #FB
Get #FB, 1, Ind()
ReDim FileData(1 To Ind(0))
Get #FB, , FileData()
Close #FB
'Whatever you want to do with the code
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|