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 " " = 7 ascii chars = 7 bytes (or more) 1 int = 4 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
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
Cast Types Static_cast sanity checks types Reinterpret_cast sanity checks size C-Style cast picks whichever
Results Outputting ascii followed by bits for 15 (F) Hex editor: Good text editor:Notepad:
Size Int = 4 bytes
Results Outputting 15 (F 16 ) followed by 258 ( )
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
Results Outputting 15 (F 16 ) followed by 258 ( ) Little Endian ArrangementMeaning 0F00 0F Little Endian ArrangementMeaning
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