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

Summing the first bytes of a file 1

Status
Not open for further replies.

BaDi

Programmer
May 14, 2002
32
0
0
NL
Hi!

How can I sum in VB the first bytes of a file?

Thanx in advance
 
Can you explain what you are trying to do a little better. What kind of file? Do you just want to open it as a binary file? How many bytes? Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Well,

This is a form of security so that every time when I open the file with my program I can check if the sum of the original file equals the new sum (it could be that the user replaced the file and then renamed it to the origins name). I guess this can be done with any kind of file. I want to eg. sum the first 400 bytes.
If I open it as a binary file? Will I be able to do it then. And if so .. how?

I hope this explained my question a little bit more.

Thanx in advance!
 
Hi,

Yes. Open the file for binary access read.
dim an array as byte() and redim it (400)
Use get to read the data directly into the array.
Loop through the array to sum the bytes.

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Thanx for your help!

But.. I have tried a couple of ways to open this file for binary read. And then place the bytes into the array.. but it doesn't realy work.

Could you please give me an example of how to implement this?

Thanx in advance!
 
I think you are trying to accomplish a CRC check on the file for registration purposes. I have found some really good crc code on .

If the file is not too big (less than 100k) you can crc the whole thing and verify that checksum with a pre-recorded one. Troy Williams B.Eng.
fenris@hotmail.com
 
Hi,

Fenris's probably got a good solution there - I haven't checked it.

I was thinking of something like:
-----------------------------------------------------------
Dim Inf As Byte, D() As Byte, s As Long, i As Long

ReDim D(400)
Inf = FreeFile
Open "c:\tmp\test.txt" For Binary Access Read As #Inf
Get #Inf, , D
Close #Inf
s = 0
For i = 0 To UBound(D)
s = s + D(i)
Next i
MsgBox s
----------------------------------------------------------- Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top