Reading Binay Data from a file.....

b_harpal
b_harpal used Ask the Experts™
on
Hello,
      I have a file containing structure type data in binary format.
I open that file in binary mode for reading. When I read a record from
file using Get(#1, ,dfheader) I'am not able to retrive the data properly.

OR if i use,

dfheader = InputB(LenB(dfheader), #1)   statement it gives me following error.

"Only public user defined types defined in public object modules can be coerced
to or from a variant or passed to late bound functions"


where dfheader is my structure defined in module as public


Public Type time_t
    tm_sec As Integer      '        seconds after the minute (from 0)
    tm_min As Integer      '        minutes after the hour (from 0)
    tm_hour As Integer      '       hour of the day (from 0)
    tm_mday As Integer      '       day of the month (from 1)
    tm_mon As Integer      '        month of the year (from 0)
    tm_year As Integer      '       years since 1900 (from 0)
    tm_wday As Integer      '       days since Sunday (from 0)
    tm_yday As Integer      '       day of the year (from 0)
    tm_isdst As Integer      '      Daylight Saving Time flag
End Type

' TIME VARIABLES       (long: no of secs since 00:00:00 1 Jan 1970) */
  Public start_time As time_t, block_time As time_t, point_time As time_t
  Public disptime(30) As String



  Public Type datafilehead
    prog_id(12) As Byte
    data_type As Byte
    verno(6) As Byte
    patient_name(30) As Byte
    patient_id(12) As Byte
    dob(12) As Byte
    height(6) As Byte
    weight(6) As Byte
    start_data_file As time_t
    data_last_edited As time_t
    comment(45) As Byte
  End Type
   
 Public dfheader As datafilehead


'*****************************************************************************************

Public Function ReadDataBlock(ByVal store_offs As Integer, ByVal bln As Integer, ByVal dat_typ As Integer) As Integer
    Dim n_pts As Integer
    n_pts = 0
    Dim ltmp As Long
    Close #1
   
    Open "c:\tone\data\test.vgs" For Binary Access Read As #1
    Seek #1, LenB(dfheader)
   
    'n_pts = InputB(2, #1)
   
    If (n_pts <= 0 Or EOF(1)) Then Exit Function
   
    'Seek #1, LenB(n_pts)
    ltmp = InputB(4, #1)    ' cum time in ms at beginning of block */
 
    rint(store_offs) = InputB(2 * n_pts, #1)   'fread(&rint[store_offs], 2, n_pts, in);
   
    If (dat_typ And &H2) Then
        cvt_x100(store_offs) = InputB(2 * n_pts, #1) 'fread(&cvt_x100[store_offs], 2, n_pts, in);
    End If
   
    If (dat_typ And &H4) Then
        SBP_x10(store_offs) = InputB(2 * n_pts, #1)  'fread(&SBP_x10[store_offs], 2, n_pts, in);
        DBP_x10(store_offs) = InputB(2 * n_pts, #1)  'fread(&DBP_x10[store_offs], 2, n_pts, in);
        MBP_x10(store_offs) = InputB(2 * n_pts, #1)  'fread(&MBP_x10[store_offs], 2, n_pts, in);
    End If
   
    If (dat_typ And &H8) Then
        Contr_x1(store_offs) = InputB(2 * n_pts, #1)  'fread(&Contr_x1[store_offs], 2, n_pts, in);
        PEP_x10(store_offs) = InputB(2 * n_pts, #1)  'fread(&PEP_x10[store_offs], 2, n_pts, in);
    End If
   
    If (dat_typ And &H10) Then
        pO2_x10(store_offs) = InputB(2 * n_pts, #1)  'fread(&pO2_x10[store_offs], 2, n_pts, in);
        pCO2_x10(store_offs) = InputB(2 * n_pts, #1)  'fread(&pCO2_x10[store_offs], 2, n_pts, in);
    End If
    If (Not bln) Then block_sum = ltmp
    ReadDataBlock = n_pts
End Function



'********************************************************************

Please tell me how to retrive binary data from file?


It's bit Urgent for me..I'am Stuck.!!

Thanks...

Cheers

harry
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dirk HaestProject manager

Commented:
Have you tried this:

fh = freefile()
Open "c:\tone\data\test.vgs" For Binary Access Read As fh

get fh,,dfheader
(or: get fh,0,dfheader)


Perhaps it doesn't succeed because of "#" ?

Commented:
I think you need to change your open mode from binary to random which is the mode for record based I/O.

Then you should be able to get / put the whole structure with a single get / put statement specifying the record number you're after in the file.

Commented:
works fine with me. If you want to read your fileheader:



Private Sub Form_Load()

 With dfheader
    .prog_id(0) = 1
    .prog_id(1) = 2
    .data_type = 3
    .verno(0) = 4
    .verno(1) = 5
    .patient_name(0) = 6
    .patient_name(1) = 7
    .patient_id(0) = 8
    .patient_id(1) = 9
    .dob(0) = 10
    .height(0) = 11
    .weight(0) = 12
   
    With .start_data_file
        .tm_hour = 13
        .tm_min = 14
    End With
   
    With .data_last_edited
        .tm_hour = 15
        .tm_min = 16
    End With
   
    .comment(0) = 17

 
 End With
 
 Open "c:\test.bin" For Binary Access Write As #1
 Put #1, 1, dfheader
 Close #1
 
 Dim dfreadheader As datafilehead
 
 Open "c:\test.bin" For Binary Access Read As #1
 Get #1, 1, dfreadheader
 Close #1
 
End Sub


************************''

Step inside the code and look for values in dfheader and dfreadheader. They are equal
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Dear grutter,

I have checked it out with Random also. But no success. The Data file already exists. It was created using 'C' FileI/O. Now are trying to open that binary file using VB.

Regards


Commented:
Dear b_harpal

In your original example you mention 2 different problems The example using InputB to read the whole structure won't work because to assign to dfheader the data type must be the structure you have created and inputB will return just a set of bytes.

The get and put method is the right way to go. You say you can't read it correctly, does the code actualy execute ok ?
What happens ?

There are data type differences between C and basic so you must ensure you get the matching types.

I suggest you try to use Put # from vb to see if the file created matches the one you have made in C. (Use a hex editor to find out) You have integers in your structure which are 2 bytes long in VB but in c integer is 4 bytes. You need to use the long data type in vb to read an integer created in C. (or change the C program to short)

Author

Commented:
Thanks a Lot....!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial