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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Incorrect Binary File Size 1

Status
Not open for further replies.

EnCocytus

Programmer
Dec 18, 2002
24
US
I am currently trying to create a file that is exactly 1 Kb in size. So I created a file and opened it in binary mode.Next I fill the file with data in a for loop that runs 1024 times. Then I close the file. But when I check the file properties to seehow large the file is, Windows shows that there is 1,047 bytes in it. I've even tried running the loop 1001 times, and the size of the file after that is 1,048 bytes.
What is going on?

this is a sample of my code.
Code:
	dim buffer
	dim bytecounter

	buffer = spaces(1)
	'put one byte of data in buffer

        For byteCounter = 0 To 1024
           Put #filenumber, , buffer
        Next
oh yeah, and the incorrect file size is the actual file size and not the size on disk size
1001100 1110101 1101011 1100101
 
A few comments that may help

If the file already exists when its opened binary it will retain its original length if longer than 1024.

dim buffer as string

typo? spaces(1) is not valid should be space(1) or space$(1)

the loop 0 to 1024 will write 1025 characters



cjw
 
Yeah, space$ was just a typo in my post. I changed the loop to
for 0 to 1023 and I also add the following code
Code:
      If FileExists(fullPath) Then
           If MsgBox("File Already Exists.  Overwrite?", vbInformation + vbOKCancel, fullPath) And vbOK Then
                Kill fullPath
           Else
                Err.Raise 1234, , "Did not overwrite file, transfer aborted"
           End If
       End If
Code:
Private Function FileExists(strPath As String) As boolean
    Dim lngRetVal As Long
    On Error Resume Next
    lngRetVal = Len(Dir$(strPath))
    If Err Or lngRetVal = 0 Then
        FileExists = False
    Else
        FileExists = True
    End If
End Function

fullPath is the same string I use to open the file, and I don't open or write to the file anywhere else in the program. However, i'm still getting the same results. Is it perhaps an EOF character thats giving me these results? 1001100 1110101 1101011 1100101
 
Did you apply my second point ?

Buffer must be explicitly defined as a string, not a variant.

cjw
 
yea, I did. And still no change. Sorry, I forgot to mention that in my post above. 1001100 1110101 1101011 1100101
 
Sorry no more ideas - anyway for what its worth heres my code and a few comments.

Dim buffer As String
Dim bytecounter
Dim filenumber As Long

filenumber = FreeFile
buffer = Space(1024)
Open "c:\xxx.txt" For Binary As #filenumber
Put #filenumber, , buffer
Close #filenumber
Debug.Print FileLen("c:\xxx.txt")

It always shows 1024 and when I check the properties the size is 1024 if buffer is a variant the size increases as expected to 1028. Using your loop style code the length is still 1024 unless buffer is a variant then the length grows to 5120.

As your file does not change size my final thought is are you sure you are checking the right file ?

cjw
 
SOE's code (not to be confused with Sony Online Entertainment ;)) works correctly. I started fooling around with Binary read/write since I never really bothered before, and I ran into a problem which I will explain in a second. Here's the code:
Code:
Private Sub Form_Load()
Dim Buffer As String
    Me.Show
    RichTextBox1.LoadFile "C:\windows\desktop\Program.exe"
    MsgBox "Load complete"
    Buffer = RichTextBox1.Text
        Open "c:\windows\desktop\Hello.exe" For Binary As #1
            Put #1, , Buffer
        Close #1
    MsgBox "Save complete"
    Debug.Print "Program.exe : " & FileLen("c:\windows\desktop\Program.exe")
    Debug.Print "Hello.exe : " & FileLen("c:\windows\desktop\Hello.exe")
End Sub
I thought the code above would effectively make a copy of Program.exe and name it Hello.exe, but this is not the case. Hello.exe seems to have the same exact data as Program.exe when viewed in Wordpad, but the file size is always 0.3MB bigger. Please note that I do NOT wish to use FileCopy(), and instead I am experimenting with binary read/write. If anyone could let me know what I'm doing wrong, I would very much appreciate it :)
 
Hmmmm, Supra
I tried your code out, and it seemed to work fine for me. File lengths and data were the same in both files. Perhaps, it is possible, that by grabbing the data in an RTF format is distorting it in some way. Unfortunately, I'm not very exerienced with RTF's. I modified your code just a little to circumvent that problem. (If that's it).


Private Sub Form_Load()

Dim Buffer As String
Me.Show
RichTextBox1.LoadFile "c:\windows\desktop\Program.exe"
MsgBox "Load complete"
Buffer = Space(FileLen("c:\windows\desktop\Program.exe"))
Open "c:\windows\desktop\Hello.exe" For Binary As #1
Open "c:\windows\desktop\Program.exe" For Binary As #2
Get #2, , Buffer

Put #1, , Buffer
Close #1
Close #2
MsgBox "Save complete"
Debug.Print "Program.exe : " & FileLen("c:\windows\desktop\Program.exe")
Debug.Print "Hello.exe : " & FileLen("c:\windows\desktop\Hello.exe")
End Sub


Also, if you really need to know what the differences in your two files are, I have some code I can email you that will create an .html sheet showing all the bytes in the two progs. and highlite the differences

Hope that helps
1001100 1110101 1101011 1100101
 
Your code works and removes the need to even use the RichTextBox control :) Thank you EnCocytus - this definitely helps me understand more about binary reading and writing. You get a star from me :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top