Chapter 8 Binary Files 1 Decimal vs Binary
Chapter 8 Binary Files 1
Decimal vs. Binary vs. Hexadecimal vs. Octal • 65 == 0 b 01000001 == 0 x 41 == 0 o 101 • If you store 65 as a character array “ 65”, you need 3 bytes. • If you store 65 as a string object, you need a pointer plus a dynamically allocated space to store “ 65 ”, at least 8 + 3 = 11 bytes. • If you store 65 as an unsigned char, you need only 1 byte! 2
Text Files vs. Binary Files • Text files • Human readable • Binary files • Not easy for human to read • Try to calculate the multiplication of 0 b 01000001 and 0 b 00101010 • Occupy more space • Your program need to be converted text data to the internal binary representation in computers • Smaller • Faster 3
Exercise: Generating a Text File • Design a program to generate 100 random integers between 0 and 100. Write those integers into a file “test. txt”. • Design another program to read out those 100 integers, and print them out. • What is the size of the file? 4
Write an Integer into a File #include <fstream> using std: : ofstream; Isn’t there #include <iostream>? int main() { ofstream outfile("test. dat", std: : ios: : binary); char c = 65; outfile. write(&c, 1); outfile. close(); return 0; } 5
od - octal dump 6
Exercise: Generating a Binary File • Design a program to generate 100 random integers between 0 and 100. Write those integers into a binary file “test. dat”. • What is the size of this file? 7
Reading a Binary File #include <iostream> #include <fstream> using std: : ifstream; using std: : cout; using std: : endl; int main() { ifstream infile("test. dat", std: : ios: : binary); char c; while ( infile. read(&c, 1) ) cout << static_cast<short>(c) << endl; infile. close(); return 0; } 8
Writing Short Integers to a Binary File #include <fstream> using std: : ofstream; int main() { ofstream outfile("test. dat", std: : ios: : binary); short a = 0 x 1234; short b = 0 x 5678; outfile. write(&a, 2); outfile. write(&b, 2); cannot initialize a parameter outfile. close(); return 0; type 'const char *' } with an rvalue of of type 'short *' 9
reinterpret_cast #include <fstream> using std: : ofstream; int main() { ofstream outfile("test. dat", std: : ios: : binary); short a = 0 x 1234; short b = 0 x 5678; outfile. write( reinterpret_cast<const char *>(&a), 2); outfile. write( reinterpret_cast<const char *>(&b), 2); outfile. close(); return 0; } 10
4 -Byte Integers • 466010 = 0 x 1234 (123416) • How will it be stored in memory? Big-Endian 0 x. B 000 0 x. B 001 0 x. B 002 0 x. B 003 00 00 12 34 Little-Endian 00 00 12 34 0 x. B 000 0 x. B 001 0 x. B 002 0 x. B 003 34 12 00 00 11
Big-Endian vs. Little Endian • Big Endian • IBM S/390, Motorola 68 k, Sun SPARC, • “Big Endian is Effectively Dead” • Little Endian • Intel x 86 • Bi-Endian • Power. PC, ARM On your host, can you write a program to inspect whether it stores integers in big-endian or littleendian? 4660 ffffe 7 e 8 34 ffffe 7 e 9 12 ffffe 7 ea 0 ffffe 7 eb 0 12
Bitmap File Format • Let’s try to read a binary file with structures. • The BMP file format ===== Bit. Map. File. Header ===== Signature: BM Filesize: 3262 Data Offset: 62 ===== Info. Header ===== size of Info. Header: 40 width: 160 height: 160 planes: 1 Bit Per Pixel: 1 compression: 0 compressed size of image: 3200 Xpiexels. Per. M: 3780 Ypiexels. Per. M: 3780 Color Used: 0 Color Important: 0 13
argc, argv #include <iostream> using std: : cout; using std: : endl; argv[0] int main(int argc, char* argv[]) { cout << "There are " << argc << " argument" << (argc>1 ? "s. " : ". ") << endl; for (int i=0 ; i<argc; i++) $ a. out This is a book. cout << argv[i] << endl; There are 5 arguments. a. out return 0; This } is a book. 14
===== Bit. Map. File. Header ===== Signature: BM Filesize: 3262 Data Offset: 62 A bit for a pixel 40 px = 40 bits = 5 bytes ===== Info. Header ===== size of Info. Header: 40 width: 160 height: 160 planes: 1 Bit Per Pixel: 1 compression: 0 compressed size of image: 3200 Xpiexels. Per. M: 3780 Ypiexels. Per. M: 3780 Color Used: 0 Color Important: 0 160 px 0 x. FF 0 x 00 15
Exercise: Generating a Bitmap File 16
- Slides: 16