1 chapter 7. objectives: you’ll learn about; introduction fundamentals of binary data files ...
TRANSCRIPT
Binary Data Files
1
CHAPTER 7
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 Files 2
Chapter 8: Binary Data Files
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 Files 3
What is a Binary File?
## 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 Files 4
Content inside Binary File: Example
## 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 Files 5
Content inside Binary File: Example (con’t)
## 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 Files 6
Content inside Binary File: Example (con’t)
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 Files 7
Binary File
Declared exactly the same way as a text file Used fopen and fclose
◦ Second argument (wb, rb) – b for binary
Chapter 7 : Binary Data Files 8
Binary File
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 Files 9
Binary File : fwrite()
#include <stdio.h>
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 Files 10
Binary File : Example 1 – creating even integers – fwrite()
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 Files 11
Binary File : fread()
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 Files 12
Binary File : Example 1- fread()
#include <stdio.h>
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 Files 13
Binary File : Example 2- fread()
Chapter 7 : Binary Data Files 14
Summary
Chapter 7 : Binary Data Files 15
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.