1 chapter 7. objectives: you’ll learn about; introduction fundamentals of binary data files ...

15
Binary Data Files 1 CHAPTER 7

Upload: brice-byrd

Post on 02-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

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

Binary Data Files

1

CHAPTER 7

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

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

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

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?

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

## 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

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

## 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)

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

## 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)

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

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

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

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

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

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()

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

#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()

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

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()

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

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()

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

#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()

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

Chapter 7 : Binary Data Files 14

Summary

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

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.