Binary files - what am I doing wrong? 
Author Message
 Binary files - what am I doing wrong?

I am using this code to create the file and put data into it...

Private Sub Command1_Click()
    Open "c:\zzz.dat" For Binary As #1
    Put #1, 5, "hello"
    Close #1
End Sub

...and I am using this code to read from the file...

Private Sub Command2_Click()
Open "c:\zzz.dat" For Binary As #1
    Dim x As String
    Get #1, 5, x
    Close #1
    MsgBox (x)
End Sub

But the MSGBOX returns a blank value...what am I doing wrong?



Thu, 03 Mar 2005 03:05:34 GMT  
 Binary files - what am I doing wrong?
Michaaal,

You need to dimension the length of your string for inputting, as below:

Private Sub Command2_Click()
Open "c:\zzz.dat" For Binary As #1
    Dim x As String*5
    Get #1, 5, x
    Close #1
    MsgBox (x)
End Sub

David Vowles

Quote:

> I am using this code to create the file and put data into it...

> Private Sub Command1_Click()
>     Open "c:\zzz.dat" For Binary As #1
>     Put #1, 5, "hello"
>     Close #1
> End Sub

> ...and I am using this code to read from the file...

> Private Sub Command2_Click()
> Open "c:\zzz.dat" For Binary As #1
>     Dim x As String
>     Get #1, 5, x
>     Close #1
>     MsgBox (x)
> End Sub

> But the MSGBOX returns a blank value...what am I doing wrong?



Thu, 03 Mar 2005 05:32:09 GMT  
 Binary files - what am I doing wrong?
Need to set X to a length first:

Private Sub Command2_Click()
Open "c:\zzz.dat" For Binary As #1
    Dim x As String
    x  = Space(5)
    Get #1, 5, x
    Close #1
    MsgBox (x)
End Sub

Max Bolingbroke


Quote:
> I am using this code to create the file and put data into it...

> Private Sub Command1_Click()
>     Open "c:\zzz.dat" For Binary As #1
>     Put #1, 5, "hello"
>     Close #1
> End Sub

> ...and I am using this code to read from the file...

> Private Sub Command2_Click()
> Open "c:\zzz.dat" For Binary As #1
>     Dim x As String
>     Get #1, 5, x
>     Close #1
>     MsgBox (x)
> End Sub

> But the MSGBOX returns a blank value...what am I doing wrong?



Thu, 03 Mar 2005 05:35:24 GMT  
 Binary files - what am I doing wrong?
Thanks alog, guys!  That worked!

...one problem, though...and maybe this would be better answered in another
thread...

I can still read my "binary" file with Notepad.  I was told that if I made a
binary file it wouldn't be able to be read with Notepad.  I need to make
this file NOT readable by notepad.

Any suggestions?


Quote:
> Michaaal,

> You need to dimension the length of your string for inputting, as below:

> Private Sub Command2_Click()
> Open "c:\zzz.dat" For Binary As #1
>     Dim x As String*5
>     Get #1, 5, x
>     Close #1
>     MsgBox (x)
> End Sub

> David Vowles


> > I am using this code to create the file and put data into it...

> > Private Sub Command1_Click()
> >     Open "c:\zzz.dat" For Binary As #1
> >     Put #1, 5, "hello"
> >     Close #1
> > End Sub

> > ...and I am using this code to read from the file...

> > Private Sub Command2_Click()
> > Open "c:\zzz.dat" For Binary As #1
> >     Dim x As String
> >     Get #1, 5, x
> >     Close #1
> >     MsgBox (x)
> > End Sub

> > But the MSGBOX returns a blank value...what am I doing wrong?



Thu, 03 Mar 2005 06:11:05 GMT  
 Binary files - what am I doing wrong?
First, the more flexible way to use a string to read in
binary material is with

    Stuff$ = Space$(40)   ' or whatever length
    GET #filnum, position, Stuff$

That way you don't need to Dim fixed-length strings.

Second, Notepad in my experience will try to swallow anything.
As long as there's ASCII data in it, it may be viewable in
Notepad.  To get around this, just XOR the data with any
kind of key upon PUTting, and then XOR it again with the
same key after GETting.

Luck,
Joe

Quote:

> Thanks alog, guys!  That worked!

> ...one problem, though...and maybe this would be better answered in another
> thread...

> I can still read my "binary" file with Notepad.  I was told that if I made a
> binary file it wouldn't be able to be read with Notepad.  I need to make
> this file NOT readable by notepad.

> Any suggestions?



> > Michaaal,

> > You need to dimension the length of your string for inputting, as below:

> > Private Sub Command2_Click()
> > Open "c:\zzz.dat" For Binary As #1
> >     Dim x As String*5
> >     Get #1, 5, x
> >     Close #1
> >     MsgBox (x)
> > End Sub

> > David Vowles


> > > I am using this code to create the file and put data into it...

> > > Private Sub Command1_Click()
> > >     Open "c:\zzz.dat" For Binary As #1
> > >     Put #1, 5, "hello"
> > >     Close #1
> > > End Sub

> > > ...and I am using this code to read from the file...

> > > Private Sub Command2_Click()
> > > Open "c:\zzz.dat" For Binary As #1
> > >     Dim x As String
> > >     Get #1, 5, x
> > >     Close #1
> > >     MsgBox (x)
> > > End Sub

> > > But the MSGBOX returns a blank value...what am I doing wrong?



Thu, 03 Mar 2005 06:16:25 GMT  
 Binary files - what am I doing wrong?
What is XOR?  I can't seem to find documentation on it?



Quote:
> First, the more flexible way to use a string to read in
> binary material is with

>     Stuff$ = Space$(40)   ' or whatever length
>     GET #filnum, position, Stuff$

> That way you don't need to Dim fixed-length strings.

> Second, Notepad in my experience will try to swallow anything.
> As long as there's ASCII data in it, it may be viewable in
> Notepad.  To get around this, just XOR the data with any
> kind of key upon PUTting, and then XOR it again with the
> same key after GETting.

> Luck,
> Joe


> > Thanks alog, guys!  That worked!

> > ...one problem, though...and maybe this would be better answered in
another
> > thread...

> > I can still read my "binary" file with Notepad.  I was told that if I
made a
> > binary file it wouldn't be able to be read with Notepad.  I need to make
> > this file NOT readable by notepad.

> > Any suggestions?



> > > Michaaal,

> > > You need to dimension the length of your string for inputting, as
below:

> > > Private Sub Command2_Click()
> > > Open "c:\zzz.dat" For Binary As #1
> > >     Dim x As String*5
> > >     Get #1, 5, x
> > >     Close #1
> > >     MsgBox (x)
> > > End Sub

> > > David Vowles


> > > > I am using this code to create the file and put data into it...

> > > > Private Sub Command1_Click()
> > > >     Open "c:\zzz.dat" For Binary As #1
> > > >     Put #1, 5, "hello"
> > > >     Close #1
> > > > End Sub

> > > > ...and I am using this code to read from the file...

> > > > Private Sub Command2_Click()
> > > > Open "c:\zzz.dat" For Binary As #1
> > > >     Dim x As String
> > > >     Get #1, 5, x
> > > >     Close #1
> > > >     MsgBox (x)
> > > > End Sub

> > > > But the MSGBOX returns a blank value...what am I doing wrong?



Thu, 03 Mar 2005 06:30:41 GMT  
 Binary files - what am I doing wrong?
Open For Binary simply tells VB what methods you intend to use when
writing/reading data from a particular file, and therefore what methods are
allowable against the file.

It does not mean that the data being written by a file opened in this manner
is done so "in binary" (thereby creating data that is obfuscated).

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


| Thanks alog, guys!  That worked!
|
| ...one problem, though...and maybe this would be better answered in
another
| thread...
|
| I can still read my "binary" file with Notepad.  I was told that if I made
a
| binary file it wouldn't be able to be read with Notepad.  I need to make
| this file NOT readable by notepad.
|
| Any suggestions?
|
|


| > Michaaal,
| >
| > You need to dimension the length of your string for inputting, as below:
| >
| > Private Sub Command2_Click()
| > Open "c:\zzz.dat" For Binary As #1
| >     Dim x As String*5
| >     Get #1, 5, x
| >     Close #1
| >     MsgBox (x)
| > End Sub
| >
| > David Vowles
| >
| >
| > > I am using this code to create the file and put data into it...
| > >
| > > Private Sub Command1_Click()
| > >     Open "c:\zzz.dat" For Binary As #1
| > >     Put #1, 5, "hello"
| > >     Close #1
| > > End Sub
| > >
| > > ...and I am using this code to read from the file...
| > >
| > > Private Sub Command2_Click()
| > > Open "c:\zzz.dat" For Binary As #1
| > >     Dim x As String
| > >     Get #1, 5, x
| > >     Close #1
| > >     MsgBox (x)
| > > End Sub
| > >
| > > But the MSGBOX returns a blank value...what am I doing wrong?
| >
|
|



Thu, 03 Mar 2005 06:56:28 GMT  
 Binary files - what am I doing wrong?


Wed, 18 Jun 1902 08:00:00 GMT  
 Binary files - what am I doing wrong?
To XOR a string, you could try something like

   Ekey$ = "DJIkjcirj78934874;fdfdfkjkdfdkljdl"
   Stuff$ = "This is my data string which may be quite long."
   For i = 1 TO LEN(Stuff$)
     j = i
     IF j > LEN(Ekey$) THEN
       j = ((j - 1) MOD LEN(Ekey$)) + 1
       ' in other words, if your key is shorter than the
       '   data you're encrypting, you'll either need to
       '   do fiddling of this nature or extend the key
       '   by something like Ekey$ = Ekey$ + Ekey$ so that
       '   you'll always have key bytes available for your
       '   data string
     END IF
     MID$(Stuff$, i) = CHR$(ASC(MID$(Stuff$, i, 1)) XOR _
                            ASC(MID$(Ekey$, j, 1)))
   Next i

Note:  This is off the top of my head after a day of fighting
off a mold count of 38,264 with antihistamines, so use at your
own risk.  Also, of course, you'll need to make sure you
align the data and the key properly when you both PUT your
data and later GET it back.

On the other hand, if you're asking what XOR _is_, it's
a bitwise operation ("eXclusive-Or") that works as follows:

      0 XOR 0 = 0
      1 XOR 0 = 1
      0 XOR 1 = 1
      1 XOR 1 = 0

And the neat thing about it is:

      If A XOR KEY = B
    Then B XOR KEY = A right back again.

The XOR operator is also commutative.

Luck,
Joe

Quote:

> What is XOR?  I can't seem to find documentation on it?



> > First, the more flexible way to use a string to read in
> > binary material is with

> >     Stuff$ = Space$(40)   ' or whatever length
> >     GET #filnum, position, Stuff$

> > That way you don't need to Dim fixed-length strings.

> > Second, Notepad in my experience will try to swallow anything.
> > As long as there's ASCII data in it, it may be viewable in
> > Notepad.  To get around this, just XOR the data with any
> > kind of key upon PUTting, and then XOR it again with the
> > same key after GETting.

> > Luck,
> > Joe


> > > Thanks alog, guys!  That worked!

> > > ...one problem, though...and maybe this would be better answered in
> another
> > > thread...

> > > I can still read my "binary" file with Notepad.  I was told that if I
> made a
> > > binary file it wouldn't be able to be read with Notepad.  I need to make
> > > this file NOT readable by notepad.

> > > Any suggestions?



> > > > Michaaal,

> > > > You need to dimension the length of your string for inputting, as
> below:

> > > > Private Sub Command2_Click()
> > > > Open "c:\zzz.dat" For Binary As #1
> > > >     Dim x As String*5
> > > >     Get #1, 5, x
> > > >     Close #1
> > > >     MsgBox (x)
> > > > End Sub

> > > > David Vowles


> > > > > I am using this code to create the file and put data into it...

> > > > > Private Sub Command1_Click()
> > > > >     Open "c:\zzz.dat" For Binary As #1
> > > > >     Put #1, 5, "hello"
> > > > >     Close #1
> > > > > End Sub

> > > > > ...and I am using this code to read from the file...

> > > > > Private Sub Command2_Click()
> > > > > Open "c:\zzz.dat" For Binary As #1
> > > > >     Dim x As String
> > > > >     Get #1, 5, x
> > > > >     Close #1
> > > > >     MsgBox (x)
> > > > > End Sub

> > > > > But the MSGBOX returns a blank value...what am I doing wrong?



Thu, 03 Mar 2005 08:01:50 GMT  
 Binary files - what am I doing wrong?


Wed, 18 Jun 1902 08:00:00 GMT  
 Binary files - what am I doing wrong?

Quote:
> Open For Binary simply tells VB what methods you intend to use when
> writing/reading data from a particular file, and therefore what methods
are
> allowable against the file.

> It does not mean that the data being written by a file opened in this
manner
> is done so "in binary" (thereby creating data that is obfuscated).

So...in other words....every time I open a VB data file like this...

open "c:\data.dat" for Input as #1
open "c:\data.dat" for Output as #1
open "c:\data.dat" for Random as #1
open "c:\data.dat" for Binary as #1

...then the file type is exactly the same?  The "Input", "Output", "Random",
and "Binary" are just methods in which I am allowed to access the file in
that instance?

Quote:

> --

> Randy Birch
> MVP Visual Basic
> http://www.mvps.org/vbnet/
> Please respond only to the newsgroups so all can benefit.



> | Thanks alog, guys!  That worked!
> |
> | ...one problem, though...and maybe this would be better answered in
> another
> | thread...
> |
> | I can still read my "binary" file with Notepad.  I was told that if I
made
> a
> | binary file it wouldn't be able to be read with Notepad.  I need to make
> | this file NOT readable by notepad.
> |
> | Any suggestions?
> |
> |


> | > Michaaal,
> | >
> | > You need to dimension the length of your string for inputting, as
below:
> | >
> | > Private Sub Command2_Click()
> | > Open "c:\zzz.dat" For Binary As #1
> | >     Dim x As String*5
> | >     Get #1, 5, x
> | >     Close #1
> | >     MsgBox (x)
> | > End Sub
> | >
> | > David Vowles
> | >

> | >
> | > > I am using this code to create the file and put data into it...
> | > >
> | > > Private Sub Command1_Click()
> | > >     Open "c:\zzz.dat" For Binary As #1
> | > >     Put #1, 5, "hello"
> | > >     Close #1
> | > > End Sub
> | > >
> | > > ...and I am using this code to read from the file...
> | > >
> | > > Private Sub Command2_Click()
> | > > Open "c:\zzz.dat" For Binary As #1
> | > >     Dim x As String
> | > >     Get #1, 5, x
> | > >     Close #1
> | > >     MsgBox (x)
> | > > End Sub
> | > >
> | > > But the MSGBOX returns a blank value...what am I doing wrong?
> | >
> |
> |



Thu, 03 Mar 2005 08:18:15 GMT  
 Binary files - what am I doing wrong?


Wed, 18 Jun 1902 08:00:00 GMT  
 Binary files - what am I doing wrong?
Isn't there a String that I can use to make notepad think that the first
line is the
end of the file?

Is seems like I used to do this a real long time ago (on my Apple II GS)



Quote:
> To XOR a string, you could try something like

>    Ekey$ = "DJIkjcirj78934874;fdfdfkjkdfdkljdl"
>    Stuff$ = "This is my data string which may be quite long."
>    For i = 1 TO LEN(Stuff$)
>      j = i
>      IF j > LEN(Ekey$) THEN
>        j = ((j - 1) MOD LEN(Ekey$)) + 1
>        ' in other words, if your key is shorter than the
>        '   data you're encrypting, you'll either need to
>        '   do fiddling of this nature or extend the key
>        '   by something like Ekey$ = Ekey$ + Ekey$ so that
>        '   you'll always have key bytes available for your
>        '   data string
>      END IF
>      MID$(Stuff$, i) = CHR$(ASC(MID$(Stuff$, i, 1)) XOR _
>                             ASC(MID$(Ekey$, j, 1)))
>    Next i

> Note:  This is off the top of my head after a day of fighting
> off a mold count of 38,264 with antihistamines, so use at your
> own risk.  Also, of course, you'll need to make sure you
> align the data and the key properly when you both PUT your
> data and later GET it back.

> On the other hand, if you're asking what XOR _is_, it's
> a bitwise operation ("eXclusive-Or") that works as follows:

>       0 XOR 0 = 0
>       1 XOR 0 = 1
>       0 XOR 1 = 1
>       1 XOR 1 = 0

> And the neat thing about it is:

>       If A XOR KEY = B
>     Then B XOR KEY = A right back again.

> The XOR operator is also commutative.

> Luck,
> Joe


> > What is XOR?  I can't seem to find documentation on it?



> > > First, the more flexible way to use a string to read in
> > > binary material is with

> > >     Stuff$ = Space$(40)   ' or whatever length
> > >     GET #filnum, position, Stuff$

> > > That way you don't need to Dim fixed-length strings.

> > > Second, Notepad in my experience will try to swallow anything.
> > > As long as there's ASCII data in it, it may be viewable in
> > > Notepad.  To get around this, just XOR the data with any
> > > kind of key upon PUTting, and then XOR it again with the
> > > same key after GETting.

> > > Luck,
> > > Joe


> > > > Thanks alog, guys!  That worked!

> > > > ...one problem, though...and maybe this would be better answered in
> > another
> > > > thread...

> > > > I can still read my "binary" file with Notepad.  I was told that if
I
> > made a
> > > > binary file it wouldn't be able to be read with Notepad.  I need to
make
> > > > this file NOT readable by notepad.

> > > > Any suggestions?



> > > > > Michaaal,

> > > > > You need to dimension the length of your string for inputting, as
> > below:

> > > > > Private Sub Command2_Click()
> > > > > Open "c:\zzz.dat" For Binary As #1
> > > > >     Dim x As String*5
> > > > >     Get #1, 5, x
> > > > >     Close #1
> > > > >     MsgBox (x)
> > > > > End Sub

> > > > > David Vowles


> > > > > > I am using this code to create the file and put data into it...

> > > > > > Private Sub Command1_Click()
> > > > > >     Open "c:\zzz.dat" For Binary As #1
> > > > > >     Put #1, 5, "hello"
> > > > > >     Close #1
> > > > > > End Sub

> > > > > > ...and I am using this code to read from the file...

> > > > > > Private Sub Command2_Click()
> > > > > > Open "c:\zzz.dat" For Binary As #1
> > > > > >     Dim x As String
> > > > > >     Get #1, 5, x
> > > > > >     Close #1
> > > > > >     MsgBox (x)
> > > > > > End Sub

> > > > > > But the MSGBOX returns a blank value...what am I doing wrong?



Thu, 03 Mar 2005 08:19:16 GMT  
 Binary files - what am I doing wrong?


Wed, 18 Jun 1902 08:00:00 GMT  
 Binary files - what am I doing wrong?


Quote:
>So...in other words....every time I open a VB data file like this...

>open "c:\data.dat" for Input as #1
>open "c:\data.dat" for Output as #1
>open "c:\data.dat" for Random as #1
>open "c:\data.dat" for Binary as #1

>...then the file type is exactly the same?  The "Input", "Output", "Random",
>and "Binary" are just methods in which I am allowed to access the file in
>that instance?

Yes. There is no such thing as a "file type" at the system level.
A file is simply a bunch of bytes that are placed in disk sector(s)
neatly one byte after the other (or not, but that's not important :)).

Just think of files on disk as a byte array.

When you open a file, you tell both the I/O subsystem and  VB's
RTL(Run Time Library) how to handle the data stream.
The I/O is responsible for handling the data exchange between the disk
and your application memory. VB's RTL is responsible for the way it
will handle the incoming or outgoing data.

Witn Input the I/O sets up a read buffer and connect your data stream
to this file's buffer.  With Output it's the reverse, a write buffer.
VB in turn is instructed to format according to what you read into.
Ie: if you read into a long, VB fetches 4 bytes.
However you can not specify write positions, as this is called a
sequential file. Of course you can reposition by using Seek()

If you open Binary, the I/O sets up both a read and a write buffers.
VB is instructed to get and put raw byte data from random positions.
As with Input/Output, you specify the number of bytes implicitly, by
the size of the variable type you read into or write out from.
You however have th eoption to also define the read and wiite start
positions manually.
put #1, startpos, data ' will write at offset startpos
put #1, , data 'will write at where ever the file pointer happens to
be pointing to.

If you open in Random, the I/O sets up the same read and write
buffers, however VB will fetch or put the contents of the file in
grabs of predefined record sizes of whatever you've specified.  ie:
len(myUdt) This simply means that when you write to record 4, the file
pointer will advance 4 * recordlength bytes from BOF.

HTH
--
Regards, Frank



Thu, 03 Mar 2005 09:58:06 GMT  
 
 [ 31 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. Recordset: What am I doing wrong?

2. What am I doing wrong??

3. What am I doing wrong?

4. Help with syntax. What am I doing wrong

5. What am I doing wrong?

6. What am I doing wrong?

7. What am I doing wrong?

8. Please tell me what I am doing wrong - DAO parameter Append Query :(

9. What am I doing wrong? -MultiSelect List box

10. Select Case - What am I doing wrong?

11. What am I doing wrong (easy).

12. OLE problem, or what am I doing wrong?

 

 
Powered by phpBB® Forum Software