?
Solved

Binary File Access

Posted on 1998-10-21
9
Medium Priority
?
221 Views
Last Modified: 2013-11-13
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
0
Comment
Question by:jaalex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 1

Accepted Solution

by:
steve06 earned 600 total points
ID: 1440999
jaalex,

To work with a binary file, open it the following way:

Open "Myfile.ZIP" for binary as #1

Then, to read the nth byte, use the command:

Dim bytMyVariable as Byte
Get #1, n, bytMyVariable

To create a binary file, use the Put command:
Put #1, n, bytMyVariable

Last but not least, if you repalce a single byte variable by a fixed-length (important!) string, then you can read or write up to 32,767 bytes at a time.

If you need more info, please let me know.
0
 
LVL 12

Expert Comment

by:mark2150
ID: 1441000
You *definiately* do NOT want to read a file *ONE BYTE* at a time. It will be slow as *MUD*. This is due to the overhead of multiple calls. Generally you want to read in blocks that are the same size as a disk cluster. This will give overall best performance from the heads of the drive up to your app. The disk controller is going to read a cluster at a time no matter what so you might as well keep your disk buffers the same size range. On the *last* cluster your returned string will be *shorter* than your cluster size (this is one way to tell you're at the end).

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

0
 

Author Comment

by:jaalex
ID: 1441001
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Expert Comment

by:mark2150
ID: 1441002
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
.

0
 

Author Comment

by:jaalex
ID: 1441003
Would you want to use LOF for this???
0
 

Author Comment

by:jaalex
ID: 1441004
Would you want to use LOF for this???
0
 

Author Comment

by:jaalex
ID: 1441005
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.
0
 

Author Comment

by:jaalex
ID: 1441006
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.
0
 
LVL 12

Expert Comment

by:mark2150
ID: 1441007
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
'

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question