Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing and Reading Raw Data

Similar presentations


Presentation on theme: "Writing and Reading Raw Data"— Presentation transcript:

1 Writing and Reading Raw Data
Binary IO Writing and Reading Raw Data

2 Files Two major flavors of file: Text Binary

3 Text Files Text files Easier to read by hand
Easier to reverse engineer Easier to hand edit More portable

4 Binary Files Binary files More compact Faster
" " = 12 ascii chars = 96 bytes 1 double = 8 bytes Faster No conversions from strings to numbers Easier random access Known size for numerics

5 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);

6 Working in Binary Mode Binary IO : n bytes starting at address s
Address expressed as char * char = 1 byte

7 Writing Write c-string:

8 Results Outputting ascii chars… Hex editor: Good text editor: Notepad:

9 Writing Non-Chars Write other types: Get pointer to data
Cast as a char* Use sizeof( ) to calculate number of bytes

10 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);

11 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);

12 Results Outputting ascii followed by bits for 15 (F) Hex editor:
Good text editor: Notepad:

13 Size Int = 4 bytes

14 Results Outputting 15 (F16) followed by 258 (10216)

15 Endianess Endianess : bytes order of a word in main memory

16 Little vs Big Endian Big is "Normal": Little weird Words in order
Bytes in a word backwards

17 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

18 Reading Need to read string, 2 ints String unknown length

19 Reading Read string char by char:

20 Reading Same, using c-string

21 Reading Reading two ints:

22 Complex Files Graphic/Sound/etc… files have defined structure:

23 Other Option Structured text : XML Less efficient than text
Machine parseable Wide collection of tools


Download ppt "Writing and Reading Raw Data"

Similar presentations


Ads by Google