Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CHAPTER 7. Objectives: You’ll learn about;  Introduction  Fundamentals of binary data files  Processing binary files  Files with mixed type data.

Similar presentations


Presentation on theme: "1 CHAPTER 7. Objectives: You’ll learn about;  Introduction  Fundamentals of binary data files  Processing binary files  Files with mixed type data."— Presentation transcript:

1 1 CHAPTER 7

2 Objectives: You’ll learn about;  Introduction  Fundamentals of binary data files  Processing binary files  Files with mixed type data  Related example Chapter 7 : Binary Data Files2

3  In the binary data file, the information will be stored in groups of binary digits. Each binary digit is a zero or one and eight binary digits grouped together is a byte. Chapter 7 : Binary Data Files3

4 ## source file: ## ## Four score and seven years ago, ## our fathers brought forth on this continent ## a new nation, conceived in liberty ## and dedicated to the proportion that ## all men are created equal. ## Chapter 7 : Binary Data Files4

5 ## binary file: ## ## 01000110 01101111 01110101 01110010 00100000 ## 01110011 01100011 01101111 01110010 01100101 ## 00100000 01100001 01101110 01100100 00100000 ## 01110011 01100101 01110110 01100101 01101110 ## 00100000 01111001 01100101 01100001 01110010 ## 01110011 00100000 01100001 01100111 01101111 ## 00101100 00001010 01101111 01110101 01110010 ## 00100000 01100110 01100001 01110100 01101000 ## 01100101 01110010 01110011 00100000 01100010 ## 01110010 01101111 01110101 01100111 01101000 ## 01110100 00100000 01100110 01101111 01110010 ## 01110100 01101000 00100000 01101111 01101110 ## 00100000 01110100 01101000 01101001 01110011 ## 00100000 01100011 01101111 01101110 01110100 ## 01101001 01101110 01100101 01101110 01110100 ## 00001010 01100001 00100000 01101110 01100101 ## 01110111 00100000 01101110 01100001 01110100 ## 01101001 01101111 01101110 00101100 00100000 ## 01100011 01101111 01101110 01100011 01100101 ## 01101001 01110110 01100101 01100100 00100000 ## 01101001 01101110 00100000 01101100 01101001 ## 01100010 01100101 01110010 01110100 01111001 ## 00001010 01100001 01101110 01100100 00100000 ## 01100100 01100101 01100100 01101001 01100011 ## 01100001 01110100 01100101 01100100 00100000 ## 01110100 01101111 00100000 01110100 01101000 ## 01100101 00100000 01110000 01110010 01101111 ## 01110000 01101111 01110011 01110100 01101001 ## 01101111 01101110 00100000 01110100 01101000 ## 01100001 01110100 00001010 01100001 01101100 ## 01101100 00100000 01101101 01100101 01101110 ## 00100000 01100001 01110010 01100101 00100000 ## 01100011 01110010 01100101 01100001 01110100 ## 01100101 01100100 00100000 01100101 01110001 ## 01110101 01100001 01101100 00101110 00001010 ## Chapter 7 : Binary Data Files5

6 ## decoded binary: ## ## Four score and seven years ago, ## our fathers brought forth on this continent ## a new nation, conceived in liberty ## and dedicated to the proportion that ## all men are created equal. Chapter 7 : Binary Data Files6

7 Binary files have two features that distinguish them from text files: Can jump instantly to any record in the file, and change the contents of a record anywhere in the file at any time. Binary files also usually have faster read and write times than text files. It is because a binary image of the record is stored directly from memory to disk (or vice versa). In a text file, everything has to be converted back and forth to text, and this takes time. Chapter 7 : Binary Data Files7

8  Declared exactly the same way as a text file  Used fopen and fclose ◦ Second argument (wb, rb) – b for binary Chapter 7 : Binary Data Files8

9  fwrite moves the block of bytes from memory to the file.  Used fwrite with four arguments: ◦ Address of the first memory cell to be copied to file ◦ No of bytes to copied to file ◦ Number of values  1 – one integer at a time  Array size - Entire size of array ◦ File pointer to the file being created Chapter 7 : Binary Data Files9

10 #include void main(){ FILE *binaryp; int i; binaryp=fopen("nums.bin","wb"); for (i=2;i<500;i+=2) fwrite(&i,sizeof(int),1,binaryp); fclose(binaryp); //printf("An integer requires %d bytes", sizeof(int)); - samples to indicate bytes occupied } Chapter 7 : Binary Data Files10

11  Used fread with four arguments: ◦ a memory address ◦ the number of bytes to read per block ◦ the number of blocks to read ◦ the file variable  Example: ◦ Thus, the line fread(&r,sizeof(struct rec),1,f); says to read 12 bytes (the size of rec) from the file f (from the current location of the file pointer) into memory address &r. One block of 12 bytes is requested. ◦ It would be just as easy to read 100 blocks from disk into an array in memory by changing 1 to 100. Chapter 7 : Binary Data Files11

12 How to read Binary Data File in C (Sequentially) { /* 1 - Additional "b" for fopen() 2 - Usage of fread() instate of fscanf */... fp = fopen(i_filename, "rb"); // read a binary file ("r" and "b")... /* loop until all data keep into array */ while( (!feof(fp))){ /*fscanf(fp, "%f", &value); you can not use this any more for binary files. Now you have have to use fread() */ fread(&value, sizeof(int), 1, fp);... } Chapter 7 : Binary Data Files12

13 #include void main(){ FILE *binaryp; int i; binaryp=fopen("nums.bin","rb"); for (i=2;i<500;i+=2){ fread(&i,sizeof(int),1,binaryp); printf("%d\n",i); } fclose(binaryp); } Chapter 7 : Binary Data Files13

14 Chapter 7 : Binary Data Files14 Summary

15 Chapter 7 : Binary Data Files15 BINARY FILES A sequence arbitrary bytes of structured data. Not in human readable form.IMPORTANT!!  A BINARY FILE can ONLY be created from within a program.  A binary file CANNOT be viewed by an editor.  To view the contents of a binary file is to write a program (or subprogram) to read each structure and format them to screen.


Download ppt "1 CHAPTER 7. Objectives: You’ll learn about;  Introduction  Fundamentals of binary data files  Processing binary files  Files with mixed type data."

Similar presentations


Ads by Google