C++ and Binary Files

In what follows it's reported a simple C++ programs that write and read a long int and an int into a binary file. In the program list we do not manage error conditions, like file that does not exist, error on writing file, ... because out of scope for this short article, focused on reading and writing binary data on file using C++


#include <iostream>
#include <fstream>
#include <climits>
#include <string>
#include <bitset>

using namespace std;

int main()
{

    // CONSTANT
    const std::string FILE_NAME = "file.bin";

    // Variable declaration
    unsigned long lngNum   = 0;
    unsigned int  intNum   = 0;


    // Notes on consolle
    cout <<  "Total sizeof: " << sizeof(lngNum) << " + " << sizeof(intNum) << endl;


    // ******************
    // ** WRITING FILE **
    // ******************
    cout << "Writing binary file..." << endl;
    ofstream file_o (FILE_NAME, ios::binary);
    for (int i=0;  i<16; ++i){

        // Insert numbers starting from MAX ot the corresponding type
        lngNum = (ULONG_MAX-15) + i;  //ULONG_MAX
        intNum = (UINT_MAX -15) + i;  //UINT_MAX

        // Write binary file
        file_o.write (reinterpret_cast<const char *>(&lngNum), sizeof(lngNum));
        file_o.write (reinterpret_cast<const char *>(&intNum), sizeof(intNum));

        // Print out data
        cout << "lngNum decimal     = "             << lngNum             << " - intNum decimal     = " << intNum << endl;

    }
    // Closing file
    file_o.close ();


    // ******************
    // ** READING FILE **
    // ******************
    lngNum = 0;     // ... just to clear variable
    intNum = 0;

    cout << "Reading binary file..." << endl;
    ifstream file_i (FILE_NAME, ios::binary);
    if(file_i.is_open()){

        // Cycle on file
        while(true){

            // Read data into a long and int
            file_i.read(reinterpret_cast<char *>(&lngNum),sizeof(lngNum));
            file_i.read(reinterpret_cast<char *>(&intNum),sizeof(intNum));

            // Exit if EOF
            if(file_i.eof()) {
                break;
            }

            // Print out data
            cout << "lngNum = " << endl;
            cout << std::dec << "\t decimal     = " << lngNum  << endl;
            cout << std::hex << "\t hexadecimal = " << lngNum  << endl;
            cout << std::oct << "\t octal       = " << lngNum  << endl;
            cout <<             "\t binary      = " << std::bitset< sizeof(lngNum)*8 >( lngNum )       << endl;

            cout << "intNum = " << endl;
            cout << std::dec << "\t decimal     = " << intNum                                          << endl;
            cout << std::hex << "\t hexadecimal = " << intNum                                          << endl;
            cout << std::oct << "\t octal       = " << intNum                                          << endl;
            cout <<             "\t binary      = " << std::bitset< sizeof(intNum)*8 >( intNum )       << endl;

            cout << "******************************************************************************"   << endl;
        }
    }
    // Closing file
    file_i.close ();

    return 0;
}


In more details, in the first part (writing file) the two variables are written 16 times and we can think at the file as composed by sixteen “rows” each with two “columns” with the first having the “long int” value and the second having the “int” value.
In the second part (reading file) the two  variables are read “line by line” and printed out.

Take care that a binary file cannot be subdivided by “lines” or “rows”. All bits 1/0 are in sequence, so thinking at it “by row” is just a virtual way to interpret it. With some little arrangement, the script should be useful to dump binary file in different format (if the standard tools xxd, hexdump, od, … and others that for sure exist and that I do not know are not “good enough” for you :-))) ).

The output is the following:

Total sizeof: 8 + 4

Writing binary file...

lngNum decimal     = 18446744073709551600 - intNum decimal     = 4294967280
lngNum decimal     = 18446744073709551601 - intNum decimal     = 4294967281
lngNum decimal     = 18446744073709551602 - intNum decimal     = 4294967282
lngNum decimal     = 18446744073709551603 - intNum decimal     = 4294967283
lngNum decimal     = 18446744073709551604 - intNum decimal     = 4294967284
lngNum decimal     = 18446744073709551605 - intNum decimal     = 4294967285
lngNum decimal     = 18446744073709551606 - intNum decimal     = 4294967286
lngNum decimal     = 18446744073709551607 - intNum decimal     = 4294967287
lngNum decimal     = 18446744073709551608 - intNum decimal     = 4294967288
lngNum decimal     = 18446744073709551609 - intNum decimal     = 4294967289
lngNum decimal     = 18446744073709551610 - intNum decimal     = 4294967290
lngNum decimal     = 18446744073709551611 - intNum decimal     = 4294967291
lngNum decimal     = 18446744073709551612 - intNum decimal     = 4294967292
lngNum decimal     = 18446744073709551613 - intNum decimal     = 4294967293
lngNum decimal     = 18446744073709551614 - intNum decimal     = 4294967294
lngNum decimal     = 18446744073709551615 - intNum decimal     = 4294967295

Reading binary file...

lngNum = 
	 decimal     = 18446744073709551600
	 hexadecimal = fffffffffffffff0
	 octal       = 1777777777777777777760
	 binary      = 1111111111111111111111111111111111111111111111111111111111110000
intNum = 
	 decimal     = 4294967280
	 hexadecimal = fffffff0
	 octal       = 37777777760
	 binary      = 11111111111111111111111111110000
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551601
	 hexadecimal = fffffffffffffff1
	 octal       = 1777777777777777777761
	 binary      = 1111111111111111111111111111111111111111111111111111111111110001
intNum = 
	 decimal     = 4294967281
	 hexadecimal = fffffff1
	 octal       = 37777777761
	 binary      = 11111111111111111111111111110001
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551602
	 hexadecimal = fffffffffffffff2
	 octal       = 1777777777777777777762
	 binary      = 1111111111111111111111111111111111111111111111111111111111110010
intNum = 
	 decimal     = 4294967282
	 hexadecimal = fffffff2
	 octal       = 37777777762
	 binary      = 11111111111111111111111111110010
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551603
	 hexadecimal = fffffffffffffff3
	 octal       = 1777777777777777777763
	 binary      = 1111111111111111111111111111111111111111111111111111111111110011
intNum = 
	 decimal     = 4294967283
	 hexadecimal = fffffff3
	 octal       = 37777777763
	 binary      = 11111111111111111111111111110011
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551604
	 hexadecimal = fffffffffffffff4
	 octal       = 1777777777777777777764
	 binary      = 1111111111111111111111111111111111111111111111111111111111110100
intNum = 
	 decimal     = 4294967284
	 hexadecimal = fffffff4
	 octal       = 37777777764
	 binary      = 11111111111111111111111111110100
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551605
	 hexadecimal = fffffffffffffff5
	 octal       = 1777777777777777777765
	 binary      = 1111111111111111111111111111111111111111111111111111111111110101
intNum = 
	 decimal     = 4294967285
	 hexadecimal = fffffff5
	 octal       = 37777777765
	 binary      = 11111111111111111111111111110101
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551606
	 hexadecimal = fffffffffffffff6
	 octal       = 1777777777777777777766
	 binary      = 1111111111111111111111111111111111111111111111111111111111110110
intNum = 
	 decimal     = 4294967286
	 hexadecimal = fffffff6
	 octal       = 37777777766
	 binary      = 11111111111111111111111111110110
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551607
	 hexadecimal = fffffffffffffff7
	 octal       = 1777777777777777777767
	 binary      = 1111111111111111111111111111111111111111111111111111111111110111
intNum = 
	 decimal     = 4294967287
	 hexadecimal = fffffff7
	 octal       = 37777777767
	 binary      = 11111111111111111111111111110111
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551608
	 hexadecimal = fffffffffffffff8
	 octal       = 1777777777777777777770
	 binary      = 1111111111111111111111111111111111111111111111111111111111111000
intNum = 
	 decimal     = 4294967288
	 hexadecimal = fffffff8
	 octal       = 37777777770
	 binary      = 11111111111111111111111111111000
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551609
	 hexadecimal = fffffffffffffff9
	 octal       = 1777777777777777777771
	 binary      = 1111111111111111111111111111111111111111111111111111111111111001
intNum = 
	 decimal     = 4294967289
	 hexadecimal = fffffff9
	 octal       = 37777777771
	 binary      = 11111111111111111111111111111001
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551610
	 hexadecimal = fffffffffffffffa
	 octal       = 1777777777777777777772
	 binary      = 1111111111111111111111111111111111111111111111111111111111111010
intNum = 
	 decimal     = 4294967290
	 hexadecimal = fffffffa
	 octal       = 37777777772
	 binary      = 11111111111111111111111111111010
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551611
	 hexadecimal = fffffffffffffffb
	 octal       = 1777777777777777777773
	 binary      = 1111111111111111111111111111111111111111111111111111111111111011
intNum = 
	 decimal     = 4294967291
	 hexadecimal = fffffffb
	 octal       = 37777777773
	 binary      = 11111111111111111111111111111011
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551612
	 hexadecimal = fffffffffffffffc
	 octal       = 1777777777777777777774
	 binary      = 1111111111111111111111111111111111111111111111111111111111111100
intNum = 
	 decimal     = 4294967292
	 hexadecimal = fffffffc
	 octal       = 37777777774
	 binary      = 11111111111111111111111111111100
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551613
	 hexadecimal = fffffffffffffffd
	 octal       = 1777777777777777777775
	 binary      = 1111111111111111111111111111111111111111111111111111111111111101
intNum = 
	 decimal     = 4294967293
	 hexadecimal = fffffffd
	 octal       = 37777777775
	 binary      = 11111111111111111111111111111101
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551614
	 hexadecimal = fffffffffffffffe
	 octal       = 1777777777777777777776
	 binary      = 1111111111111111111111111111111111111111111111111111111111111110
intNum = 
	 decimal     = 4294967294
	 hexadecimal = fffffffe
	 octal       = 37777777776
	 binary      = 11111111111111111111111111111110
******************************************************************************
lngNum = 
	 decimal     = 18446744073709551615
	 hexadecimal = ffffffffffffffff
	 octal       = 1777777777777777777777
	 binary      = 1111111111111111111111111111111111111111111111111111111111111111
intNum = 
	 decimal     = 4294967295
	 hexadecimal = ffffffff
	 octal       = 37777777777
	 binary      = 11111111111111111111111111111111
******************************************************************************

Regarding xxd this is a short script to convert binary data interpreted as exadecimal (the first 12 byte, but you can change as you want) in decimal.

xxd -c 12 -p -u file.bin > out.sh ; sed -i -e 's/^/echo "ibase=16;/' out.sh ; sed -i -e 's/$/" | bc/' out.sh ; chmod +x out.sh; ./out.sh ; rm out.sh