jaalex
asked on
Binary File Access
I am working on a winsock program to do some data testing.
I currently have the socket part working with some text.
I would like to read in a binary file to send. I am not sure how to open the binary file (such as a zip file). I am used to working with binary formated files.
Code examples would help
I currently have the socket part working with some text.
I would like to read in a binary file to send. I am not sure how to open the binary file (such as a zip file). I am used to working with binary formated files.
Code examples would help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok I guess I have two questions.
1) How do I tell the length of the file.
2) I want to feed the to a winsock program tha I wrote. I want to be able to read in 2048 byte sections. How do I do that. Also if the program doesn't happen to end exactly at the end of the block how does the read know that.
I will up the points since I am being such a newbie on this topic
1) How do I tell the length of the file.
2) I want to feed the to a winsock program tha I wrote. I want to be able to read in 2048 byte sections. How do I do that. Also if the program doesn't happen to end exactly at the end of the block how does the read know that.
I will up the points since I am being such a newbie on this topic
filelen() will give you the length before you open the file.
Set your character buffer to 2048 like this:
IsLastBlock = False
(repeat this loop as required)
Buffer$ = Space$( 2048 ) ' Fill with 2k of empty
GET #1,,Buffer$ 'Replace with data from disk
IF LEN( Buffer$ ) <> 2048 THEN IsLastBlock = True
.
Set your character buffer to 2048 like this:
IsLastBlock = False
(repeat this loop as required)
Buffer$ = Space$( 2048 ) ' Fill with 2k of empty
GET #1,,Buffer$ 'Replace with data from disk
IF LEN( Buffer$ ) <> 2048 THEN IsLastBlock = True
.
ASKER
Would you want to use LOF for this???
ASKER
Would you want to use LOF for this???
ASKER
Ok then so I take everything in a nice blocks off 2048 bytes of information. What happens to that last little bit when I don't hage 2048 bytes of information.
ASKER
Ok then so I take everything in a nice blocks off 2048 bytes of information. What happens to that last little bit when I don't hage 2048 bytes of information.
Then you send a short block to finish up.
LOF is used when the file is OPEN. FILELEN when the file is closed.
The length of Buffer$ will be 2048 if you read a full block or something less if you're at the end of the file. Here is some nice code for binary file copying:
'
' FileSpec$ = Full name/path of original file
' filnam$ = Root file name in local directory
'
' Ok, we want to copy in large blocks for speed, but we
' can't get too big or we'll run out of buffer
'
If Len(filnam$) > 0 Then
rawsize = FileLen(filespec$)
numblocks = Int(rawsize / 4096)
fraction = rawsize Mod 4096
Open filespec$ For Binary As #1
Open maildir$ + filnam$ For Binary As #2
'
If numblocks < 1 Then GoTo copytail
'
For cindex = 1 To numblocks
bite$ = Space$(4096)
Get #1, , bite$
Put #2, , bite$
Next cindex
'
copytail:
If fraction > 0 Then
bite$ = Space$(fraction)
Get #1, , bite$
Put #2, , bite$
End If
'
Close
End If
'
LOF is used when the file is OPEN. FILELEN when the file is closed.
The length of Buffer$ will be 2048 if you read a full block or something less if you're at the end of the file. Here is some nice code for binary file copying:
'
' FileSpec$ = Full name/path of original file
' filnam$ = Root file name in local directory
'
' Ok, we want to copy in large blocks for speed, but we
' can't get too big or we'll run out of buffer
'
If Len(filnam$) > 0 Then
rawsize = FileLen(filespec$)
numblocks = Int(rawsize / 4096)
fraction = rawsize Mod 4096
Open filespec$ For Binary As #1
Open maildir$ + filnam$ For Binary As #2
'
If numblocks < 1 Then GoTo copytail
'
For cindex = 1 To numblocks
bite$ = Space$(4096)
Get #1, , bite$
Put #2, , bite$
Next cindex
'
copytail:
If fraction > 0 Then
bite$ = Space$(fraction)
Get #1, , bite$
Put #2, , bite$
End If
'
Close
End If
'
If you want to create a new file as you go you need to kill the file off explicitly prior to opening with BINARY.
M