Writing and Reading Raw Data Binary IO Writing and Reading Raw Data
Files Two major flavors of file: Text Binary
Text Files Text files Easier to read by hand Easier to reverse engineer Easier to hand edit More portable
Binary Files Binary files More compact Faster "260838.0573 " = 12 ascii chars = 96 bytes 1 double = 8 bytes Faster No conversions from strings to numbers Easier random access Known size for numerics
Working in Binary Mode Can specify binary more when opening a file //Open an output filestream binary mode ofstream file("info.dat", ios::binary); //Open a filestream using both output mode and binary fstream file2("info.dat", ios::out | ios::binary); //Open a filestream using allowing output, input both in binary fstream file3("info.dat", ios::out | ios::in | ios::binary);
Working in Binary Mode Binary IO : n bytes starting at address s Address expressed as char * char = 1 byte
Writing Write c-string:
Results Outputting ascii chars… Hex editor: Good text editor: Notepad:
Writing Non-Chars Write other types: Get pointer to data Cast as a char* Use sizeof( ) to calculate number of bytes
Cast Types Static_cast sanity checks types Reinterpret_cast sanity checks size C-Style cast picks whichever int x = 15; //Ask to do conversion if compiler knows rule to change type double y = static_cast<double>(x); //Tell compiler to ignore logic and treat bits as new type // Has to be same size double y2 = reinterpret_cast<double>(x); //C-Style : might do static, might do reinterpret double y3 = (double)(x);
Cast Types Static_cast sanity checks types Reinterpret_cast sanity checks size C-Style cast picks whichever int x = 15; //Ask to do conversion if compiler knows rule to change type char* dataPointer = static_cast<char*>(&x); //Tell compiler to ignore logic and treat bits as new type char* dataPointer2 = reinterpret_cast<char*>(&x); //C-Style : might do static, might do reinterpret char* dataPointer3 = (char*)(&x);
Results Outputting ascii followed by bits for 15 (F) Hex editor: Good text editor: Notepad:
Size Int = 4 bytes
Results Outputting 15 (F16) followed by 258 (10216)
Endianess Endianess : bytes order of a word in main memory
Little vs Big Endian Big is "Normal": Little weird Words in order Bytes in a word backwards
Little Endian Arrangement Little Endian Arrangement Results Outputting 15 (F16) followed by 258 (10216) Little Endian Arrangement Meaning 0F 00 Little Endian Arrangement Meaning 02 01 00
Reading Need to read string, 2 ints String unknown length
Reading Read string char by char:
Reading Same, using c-string
Reading Reading two ints:
Complex Files Graphic/Sound/etc… files have defined structure:
Other Option Structured text : XML Less efficient than text Machine parseable Wide collection of tools