Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone account for these extra bytes? 2

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
0
0
GB
Right, I have a byte array tmp which contains:

tmp(0) = 49
tmp(1) = 50
tmp(2) = 51
tmp(3) = 52


Then I do this:

fnum = FreeFile
Open filenamepath For Binary As fnum
Put #fnum, , tmp
Close #fnum


I then open the file in a text editor and see this:

"   1234"

where I was clearly expecting to see:

"1234"

Where are these extra bytes comming from? I think there are two bytes added to the beginning of the file. A '17' and a '32' are what they seem to be!

Any ideas on what I should do? Should I try text stream or something in the FSO instead??

Cheers

elziko
 
Elziko, I'm not suggesting what you first posted, but what you last posted without the chr() function. It writes the bytes one at a time. Strongm's StrConv eliminates the need for the loop, giving you better performance.

:cool:Your original put of a dynamic array of bytes results in the descriptor being written followed by the data. Suggested solutions include converting the array to a string and to Put the string AND to Put the bytes one at a time. Your attempt at the second method required the CHR function because it used the output file mode and the print# statement.


Another issue for Binary mode files is pre-existing data in the file. If the file starts out with 16 bytes, putting 4 bytes only changes the 4 bytes, the remaining 12 do just that, remain. Putting beyond the end of file simply extends the file.



Wil Mead
wmead@optonline.net

 
This works.
Code:
Dim aryByt() As Byte
ReDim aryByt(3)
aryByt(0) = Asc("1")
aryByt(1) = Asc("2")
aryByt(2) = Asc("3")
aryByt(3) = Asc("4")
Open "C:\test.txt" For Binary As #1
Put #1, , aryByt
Close #1
End Sub
Compare Code (Text)
Generate Sort in VB or VBScript
 
Contrary to my first post, the descriptor ONLY gets wriiten when OPEN RANDOM so something got crossed up somwhere.
"For files opened in Binary mode, all of the Random rules apply, except:

The Len clause in the Open statement has no effect. Put writes all variables to disk contiguously; that is, with no padding between records.


For any array other than an array in a user-defined type, Put writes only the data. No descriptor is written."

I tested my previous post but the final test is whether a BYTE Array gets written "as-is" as I believe it does.


Compare Code (Text)
Generate Sort in VB or VBScript
 
If you still havent got it to work i belive this will, cause you are writing it out 1 byte at a time (in binary) and not telling it to write out the whole array.

Code:
 Dim strA As String
    Dim tmp() As Byte
    Dim fnum As Long
    
    ReDim tmp(3) ' Make dynamic to emulate problem discussed
    
    tmp(0) = 49
    tmp(1) = 50
    tmp(2) = 51
    tmp(3) = 52

    fnum = FreeFile
    Open "c:\dummy.txt" For Binary As fnum
    for a=0 to ubound(tmp)
       put fnum,,tmp(a)
    next
    Close #fnum
Karl Pietri
 
Who would have thought that a few extra bytes would result in suc a long thread...
 
What's the record anyway...?

I hope this doesn't get be banned from the site!

Wil Mead
wmead@optonline.net

 
7.4 posts per byte. Not bad!

elziko
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top