FileGet() only gets zeroes

  • Follow


Hello all, I have the code listed below. This *was* working. The binary
file has this structure:

2 bytes marker bytes
2 bytes flags
4 bytes 32 bit time stamp
2 bytes
x bytes null terminated text string
x bytes The Data

The text string is optional, so it may be 0 bytes. The fourth header
byte tells me this. I tested it thoroughly, but now that I have revisted
this process the fileget() is reading all zeroes for the marker bytes and
flags. I opened the file in DEBUG and verified that the expected data
was there. Any suggestions or ideas on alternate methods are welcomed.
Thank you, Saga


    Dim TheBytes() As Byte
    Dim strName As String = ""
    Dim bytHeader(3) As Byte
    Dim bytByte As Byte
    Dim lngTimeStamp As Integer
    Dim bytExtra(1) As Byte
    Dim intFH As Integer


    intFH = FreeFile()

    FileOpen(intFH, strDataFile, OpenMode.Binary)

    FileGet(intFH, (bytHeader))

    'Check for valid marker bytes
    If bytHeader(0) = 225 And bytHeader(1) = 179 Then

      FileGet(intFH, lngTimeStamp)

      '2 more bytes.
      FileGet(intFH, (bytExtra))

      If (bytHeader(3) And 16) = 16 Then

        'Get the name.
        Do
          FileGet(intFH, bytByte)

          If bytByte > 0 Then
            strName = strName & Chr(bytByte)
          End If
        Loop Until bytByte = 0
      End If


      ReDim TheBytes(CInt(LOF(intFH) - 1))

      FileGet(intFH, (TheBytes))


0
Reply Saga 5/11/2010 7:56:08 PM

Am 11.05.2010 21:56, schrieb Saga:
> Hello all, I have the code listed below. This *was* working. 

....until you did _what_?  Enable Option Strict and put the array names
in () braces?

> The binary
> file has this structure:
> 
> 2 bytes marker bytes
> 2 bytes flags
> 4 bytes 32 bit time stamp
> 2 bytes
> x bytes null terminated text string
> x bytes The Data
> 
> The text string is optional, so it may be 0 bytes. The fourth header
> byte tells me this. I tested it thoroughly, but now that I have revisted
> this process the fileget() is reading all zeroes for the marker bytes and
> flags. I opened the file in DEBUG and verified that the expected data
> was there. Any suggestions or ideas on alternate methods are welcomed.
> Thank you, Saga

Did you upgrade a VB6 project? Or why do you use the old VB functions?
Use the System.IO namespace instead.


>     Dim TheBytes() As Byte
>     Dim strName As String = ""
>     Dim bytHeader(3) As Byte
>     Dim bytByte As Byte
>     Dim lngTimeStamp As Integer
>     Dim bytExtra(1) As Byte
>     Dim intFH As Integer
> 
> 
>     intFH = FreeFile()
> 
>     FileOpen(intFH, strDataFile, OpenMode.Binary)
> 
>     FileGet(intFH, (bytHeader))

With the "()" arround the 2nd arg, it is an expression.
The value of the expressions is stored in an internal, local variable
in order to be able to pass the reference to the variable to the
FileGet method. This is because the 2nd arg is passed ByRef.

FileGet returns a new array, and the reference is stored in that
invisible local variable. After that it's lost.

So the solution while keeping Option Strict On is:

      Dim tmpArray As Array = bytHeader
      FileGet(intFH, tmpArray)
      bytHeader = DirectCast(tmpArray, Byte())


Still I wouldn't use the FileXY methods anymore.

-- 
Armin
0
Reply Armin 5/11/2010 11:32:30 PM


Your reply is much appreciated. To answer some of your questions:

Yes - I ported this algorithm from a VB6 project. After experimenting
with various options I found that using fileX() family of functions was
the fastest way to convert the VB6 code.

Yes - I turned on Option Strict. All successful testing was done
before I did that. Suspecting that this might have caused the change in
behavior I turned option strict OFF, but this did not solve the problem.

Yes - after the code failed I am looking for an alternative to using the
fileX() family of functions. I am aware that using the current method
is notthe best way to do this. Lack of time and ease won out this time,
but I am looking for a different way to do this.

I have made the change that you suggested and the routine is now
once again functional. Thank you for you help. Your suggestions
have not fallen on deaf ear and I will look into a way to do this not
using the fileX() family. I am currently looking into using System.IO.
FileStream. Regards, Saga

"Armin Zingler" <az.nospam@freenet.de> wrote in message 
news:%23sHu7JW8KHA.1560@TK2MSFTNGP02.phx.gbl...
> Am 11.05.2010 21:56, schrieb Saga:
>> Hello all, I have the code listed below. This *was* working.
>
> ...until you did _what_?  Enable Option Strict and put the array names
> in () braces?
>
>> The binary
>> file has this structure:
>>
>> 2 bytes marker bytes
>> 2 bytes flags
>> 4 bytes 32 bit time stamp
>> 2 bytes
>> x bytes null terminated text string
>> x bytes The Data
>>
>> The text string is optional, so it may be 0 bytes. The fourth header
>> byte tells me this. I tested it thoroughly, but now that I have revisted
>> this process the fileget() is reading all zeroes for the marker bytes and
>> flags. I opened the file in DEBUG and verified that the expected data
>> was there. Any suggestions or ideas on alternate methods are welcomed.
>> Thank you, Saga
>
> Did you upgrade a VB6 project? Or why do you use the old VB functions?
> Use the System.IO namespace instead.
>
>
>>     Dim TheBytes() As Byte
>>     Dim strName As String = ""
>>     Dim bytHeader(3) As Byte
>>     Dim bytByte As Byte
>>     Dim lngTimeStamp As Integer
>>     Dim bytExtra(1) As Byte
>>     Dim intFH As Integer
>>
>>
>>     intFH = FreeFile()
>>
>>     FileOpen(intFH, strDataFile, OpenMode.Binary)
>>
>>     FileGet(intFH, (bytHeader))
>
> With the "()" arround the 2nd arg, it is an expression.
> The value of the expressions is stored in an internal, local variable
> in order to be able to pass the reference to the variable to the
> FileGet method. This is because the 2nd arg is passed ByRef.
>
> FileGet returns a new array, and the reference is stored in that
> invisible local variable. After that it's lost.
>
> So the solution while keeping Option Strict On is:
>
>      Dim tmpArray As Array = bytHeader
>      FileGet(intFH, tmpArray)
>      bytHeader = DirectCast(tmpArray, Byte())
>
>
> Still I wouldn't use the FileXY methods anymore.
>
> -- 
> Armin 


0
Reply Saga 5/12/2010 3:46:52 PM

On May 12, 10:46=A0am, "Saga" <antiS...@nowhere.com> wrote:
> Your reply is much appreciated. To answer some of your questions:
>
> Yes - I ported this algorithm from a VB6 project. After experimenting
> with various options I found that using fileX() family of functions was
> the fastest way to convert the VB6 code.
>
> Yes - I turned on Option Strict. All successful testing was done
> before I did that. Suspecting that this might have caused the change in
> behavior I turned option strict OFF, but this did not solve the problem.
>
> Yes - after the code failed I am looking for an alternative to using the
> fileX() family of functions. I am aware that using the current method
> is notthe best way to do this. Lack of time and ease won out this time,
> but I am looking for a different way to do this.
>
> I have made the change that you suggested and the routine is now
> once again functional. Thank you for you help. Your suggestions
> have not fallen on deaf ear and I will look into a way to do this not
> using the fileX() family. I am currently looking into using System.IO.
> FileStream. Regards, Saga
>
> "Armin Zingler" <az.nos...@freenet.de> wrote in message
>
> news:%23sHu7JW8KHA.1560@TK2MSFTNGP02.phx.gbl...
>
> > Am 11.05.2010 21:56, schrieb Saga:
> >> Hello all, I have the code listed below. This *was* working.
>
> > ...until you did _what_? =A0Enable Option Strict and put the array name=
s
> > in () braces?
>
> >> The binary
> >> file has this structure:
>
> >> 2 bytes marker bytes
> >> 2 bytes flags
> >> 4 bytes 32 bit time stamp
> >> 2 bytes
> >> x bytes null terminated text string
> >> x bytes The Data
>
> >> The text string is optional, so it may be 0 bytes. The fourth header
> >> byte tells me this. I tested it thoroughly, but now that I have revist=
ed
> >> this process the fileget() is reading all zeroes for the marker bytes =
and
> >> flags. I opened the file in DEBUG and verified that the expected data
> >> was there. Any suggestions or ideas on alternate methods are welcomed.
> >> Thank you, Saga
>
> > Did you upgrade a VB6 project? Or why do you use the old VB functions?
> > Use the System.IO namespace instead.
>
> >> =A0 =A0 Dim TheBytes() As Byte
> >> =A0 =A0 Dim strName As String =3D ""
> >> =A0 =A0 Dim bytHeader(3) As Byte
> >> =A0 =A0 Dim bytByte As Byte
> >> =A0 =A0 Dim lngTimeStamp As Integer
> >> =A0 =A0 Dim bytExtra(1) As Byte
> >> =A0 =A0 Dim intFH As Integer
>
> >> =A0 =A0 intFH =3D FreeFile()
>
> >> =A0 =A0 FileOpen(intFH, strDataFile, OpenMode.Binary)
>
> >> =A0 =A0 FileGet(intFH, (bytHeader))
>
> > With the "()" arround the 2nd arg, it is an expression.
> > The value of the expressions is stored in an internal, local variable
> > in order to be able to pass the reference to the variable to the
> > FileGet method. This is because the 2nd arg is passed ByRef.
>
> > FileGet returns a new array, and the reference is stored in that
> > invisible local variable. After that it's lost.
>
> > So the solution while keeping Option Strict On is:
>
> > =A0 =A0 =A0Dim tmpArray As Array =3D bytHeader
> > =A0 =A0 =A0FileGet(intFH, tmpArray)
> > =A0 =A0 =A0bytHeader =3D DirectCast(tmpArray, Byte())
>
> > Still I wouldn't use the FileXY methods anymore.
>
> > --
> > Armin

Look at the BinaryReader class.  It has methods such as ReadByte,
ReadInt32, ReadString, etc. that should work for your purposes and
it's not any more difficult than FileGet.

http://msdn.microsoft.com/en-us/library/system.io.binaryreader_methods%28v=
=3DVS.100%29.aspx

Chris
0
Reply Chris 5/12/2010 4:51:15 PM

As Chris says. And here's the code:  Untested of course! ;)

In the line "TheBytes = " I'm assuming that you want to read
only the rest of the file. In your code the array was as long
as the whole file.


imports system.io

'...

      Using fs As New FileStream("a:\devicemissing", FileMode.Open, FileAccess.Read, FileShare.Read)
         Dim br As New BinaryReader(fs)

         Dim header As Byte()
         Dim name As String
         Dim TheBytes As Byte()

         header = br.ReadBytes(4)

         If header(0) = 225 AndAlso header(1) = 179 Then
            Dim Timestamp = br.ReadInt32

            br.ReadInt16()

            If (header(3) And 16) = 16 Then
               'Get the name.
               Dim Codes As New List(Of Byte)

               Do
                  Dim code = br.ReadByte
                  If code = 0 Then Exit Do
                  Codes.Add(code)
               Loop

               name = System.Text.Encoding.Default.GetString(Codes.ToArray)
            End If

            TheBytes = br.ReadBytes(CInt(fs.Length - fs.Position))
         End If

         br.Close()
      End Using



--
Armin
0
Reply Armin 5/12/2010 6:27:38 PM

Chris and Armin - thank you both. I will implement the sample code given.
Regards, Saga


0
Reply Saga 5/12/2010 7:31:06 PM

5 Replies
193 Views

(page loaded in 8.05 seconds)

Similiar Articles:







7/29/2012 10:19:03 PM


Reply: