Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[85] Binary Files

Last post 06-21-2007 2:43 PM by chenneke. 20 replies.
Page 1 of 2 (21 items) 1 2 Next >
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [85] Binary Files

    This thread is for discussions of Binary Files.

    • Post Points: 75
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 10-23-2001 11:28 PM In reply to

    • Chocobo
    • Top 500 Contributor
    • Joined on 09-18-2001
    • Addicted Member
    • Points 715

    Questions

    Since you can do the same kind of stuff like this and save it to the registry, which is easier, why use binary files?  I could see it if you had a LOT of values, but then wouldn't you just use and INI file or something for conveienience?  I guess keeping things secret, like passwords, you might use a bin file for, but what could it be used for other than that?
    • Post Points: 0
  • 10-24-2001 6:40 AM In reply to

    • gigsvoo
    • Top 75 Contributor
    • Joined on 08-16-2001
    • Guru
    • Points 1,965
    This is a 2 ways method, if your program very much independent on settings inside the INI files you just have to read and write the INI files at all times.

    Or if ur program is mostly put the value inside the registry for a one-time settings of something in conjunction to the system setting, registry would be better.

    But i prefer using INI files since it can be separated from the syste, registry and avoid corruption.

    One more thing is in registry, you can organize ur setting in parent and child relationship but not INI file.
    • Post Points: 0
  • 12-11-2001 2:03 PM In reply to

    • NetGert
    • Not Ranked
    • Joined on 10-30-2001
    • Junior Member
    • Points 90

    Corrections!!!

    The Len() function returns null if the file is open in binary mode. Use the LOF() function instead.

    Another thing, String() function is better to use than Space() function because if you have used value bigger than new value, Space() only fills string with spaces, leaving the size as is, but String() resizes the strng.
    • Post Points: 0
  • 11-01-2002 11:09 PM In reply to

    • Osnr
    • Not Ranked
    • Joined on 11-01-2002
    • New Member
    • Points 25
    And don't forget, data files shouldn't be written to the registry.
    • Post Points: 0
  • 11-24-2002 9:16 PM In reply to

    Reading c++ made files into Vb and vice versa

    Is it possible to read a C++ written binary file into visual basic and write it again using vb then have it read by c++. If it is then can someone please tell me. also how do you write a typefef structure into a file using c++. Does anyone know. I would really appreciate it.
    • Post Points: 0
  • 12-20-2002 8:35 AM In reply to

    • dhg4
    • Not Ranked
    • Joined on 12-20-2002
    • New Member
    • Points 40

    Binary files

    Quote:
    [1]Posted by TheShotGunnner on 24 Nov 2002 09:16 PM[/1]
    Is it possible to read a C++ written binary file into visual basic and write it again using vb then have it read by c++. If it is then can someone please tell me. also how do you write a typefef structure into a file using c++. Does anyone know. I would really appreciate it.


    A binary file is a binary file regardless of which language creates it.  The following code reads in a binary file 2 bytes at a time (the read statement) and then writes them to another binary file 2 bytes at a time (the write statement)
    #include <fstream.h>
    #include <iomanip.h>
    int main()
    {
       int ind, tot;
       unsigned char byte[2];
       ifstream infile("2000_09_28_18_40_11_Sen2_Grp0.dat", ios::in );
       ofstream outfile("test.dat", ios::binary );

       for ( ind = 1; ind <= 20; ind++ )
       {
           infile.read( byte, 2 );
           outfile.write( byte, 2 );
           tot = byte[0] + ( byte[1] << 8 );
           cout << setw(8) << ind << setw(8) << tot << setw(8) << (int)byte[0] << setw(8) << (int)byte[1] << endl;
       }
       return 0;
    }

    I plan to test test.dat later and make sure that VB can read it.  But I have little doubt that it will.  You may wonder what I was writing to the screen with cout.  (This is important to consider.)    Often binary files contain 2 byte integers split into two separate bytes.  Here I was taking the bytes and recombining them into their original value. For example the first two number that are read are 208(byte[0] ) and 7 (byte[1]).  Shifting the the 1 byte 7 8 places (the equivalent of multiplying by 256) and then adding it to byte[0] to make an integer 2 bytes long equals 2000.  7 * 256 + 208 = 1792 + 208 = 2000 or looked at another way 00000111 010110000 is a 16 bit representation of 2000.

    I hope this helps.

    David
    • Post Points: 0
  • 12-20-2002 5:55 PM In reply to

    thanx
    • Post Points: 0
  • 12-26-2002 12:30 PM In reply to

    • dhg4
    • Not Ranked
    • Joined on 12-20-2002
    • New Member
    • Points 40

    Reading c++ made files into Vb and vice versa

    Hi Shotgunner,
    I assumed that a binary file that could be read by C/C++ could also be read in VB.  There are constraints on how to do that but here goes.  In the code I posted before I created a binary file.  Here's the code I used to read it and write out the equivalent integer (or more correctly long) data in VB:
    Private Sub cmdRead_Click()
       Dim ibyte(2) As String * 1
       Dim tot, temp As Long
       Dim ind As Integer
       For ind = 0 To 19
           Get #1, , ibyte(1)
           Get #1, , ibyte(2)
           temp = Asc(ibyte(2)) * 2 ^ 8
           tot = Asc(ibyte(1)) + temp
           txtData(ind * 3).Text = tot
           txtData((ind * 3) + 1).Text = Asc(ibyte(1))
           txtData((ind * 3) + 2).Text = Asc(ibyte(2))
       Next ind
    End Sub

    Private Sub mnuExitProgram_Click()
       Unload frmBin
    End Sub

    Private Sub mnuFileOpen_Click()
       Open "test.dat" For Binary As #1
       cmdRead.Enabled = True
    End Sub

    Obviously I won't be sending you the forms.
    Here's the C (or more correctly the VC++ ) code that accomplishes a similar purpose:

    #include <fstream.h>
    #include <iomanip.h>
    int main()
    {
       unsigned ind, tot;
       unsigned char byte[2];
       ifstream infile("test.dat", ios::in );

       for ( ind = 1; ind <= 20; ind++ )
       {
           infile.read( byte, 2 );
           tot = byte[0] + ( byte[1] << 8 );
           cout << setw(8) << ind << setw(8) << tot << setw(8) << (int)byte[0] << setw(8) << (int)byte[1] << endl;
       }
       return 0;
    }

    Both programs read in the binary data from test.dat and displayed (either to console or to form) the equivalent numerical data.
    • Post Points: 0
  • 03-30-2003 1:43 AM In reply to

    If everything you ever wanted to work with were an INI file, that'd be perfect. But if you want to make any sort of editor chances are you'll be using Binary file access.
    • Post Points: 0
  • 03-30-2003 1:49 AM In reply to

    Filesize?

    How do I find the size of the file?
    [edit] Nevermind. LOF(1) seems to be it.
    • Post Points: 0
  • 08-31-2003 10:19 PM In reply to

    • vivi0
    • Not Ranked
    • Joined on 08-30-2003
    • New Member
    • Points 60

    Problems...

    I don't know what I am doing wrong, but I would like to be able to read and write to *.exe and *.dll files. The problem is that it only gets the header ( MZ ). Does anyone know what I may have done wrong, or another method that can open EXE and DLL files? Thanks!

    -vivi0
    • Post Points: 0
  • 11-04-2003 1:34 PM In reply to

    • davour
    • Top 500 Contributor
    • Joined on 11-01-2003
    • Member
    • Points 375

    Error Reading Values

    I am having a trouble reading binary data

    I have the following from the tutorial:

    Quote:

    Dim nFileNum As Integer, sString As String
    nFileNum = FreeFile
    Open "C:\Example\Example.txt" For Binary Access _
      Read Lock Read Write As #nFileNum
    sString = Space$(6)
    Get #nFileNum, 1, sString
    Close #nFileNum
    MsgBox sString


    It reads the value in the file fine, but since i wrote the file in binary it reads it back in binary and all i get is a ""

    Am i missing something... does the file need to be read in a certain way?
    • Post Points: 0
  • 03-02-2004 10:38 AM In reply to

    Hi. I'm having a similar problem, I just can't get the header. I want to check if a .BMP file is in the right format. The header is BM, but i'm just getting M8 as header. That must be wrong . How do you get the header? Isn't it just Code:
    Get #FileNo, 2, sHeader
    ??
    • Post Points: 0
  • 03-02-2004 10:41 AM In reply to

    Just a tip

    Never use 1 as file number. Use
    Code:

    Dim FileNo As Integer
    FileNo = FreeFile
    Open ... ... As #FileNo
    Get #FileNo, , FileStr

    [courier new]// Mathias[/courier new]
    • Post Points: 0
Page 1 of 2 (21 items) 1 2 Next >