chapter 8 : binary data files1 binary data files chapter 8

14
Chapter 8 : Binary Data Files 1 Binary Data Files CHAPTER 8

Upload: harriet-todd

Post on 04-Jan-2016

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Chapter 8 : Binary Data Files 1

Binary Data Files

CHAPTER 8

Page 2: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Chapter 8 : Binary Data Files 2

Chapter 8: Binary Data Files

Objectives: You’ll learn about; Introduction Fundamentals of binary data files Processing binary files Files with mixed type data Related example

Page 3: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

What is a Binary File? 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 8 : Binary Data Files 3

Page 4: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Content inside Binary File: Example ## 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 8 : Binary Data Files 4

Page 5: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Content inside Binary File: Example (con’t)01000110 01101111 010101 01110010 00100000

01110011 01100011 01101111 01110010 0110010100100000 0111100001 01101110 01100100 0010000001110011 01100101 01110110 01100101 0110111000100000 01111001 01100101 01100001 0111001001110011 00100000 01100001 01100111 0110111100101100 00001010 01101111 01110101 0111001000100000 01100110 01100001 01110100 0110100001100101 01110010 01110011 00100000 0110001001110010 01101111 01110101 01100111 0110100001110100 00100000 01100110 01101111 0111001001110100 01101000 00100000 01101111 0110111000100000 01110100 01101000 01101001 0111001100100000 01100011 01101111 01101110 0111010001101001 01101110 01100101 01101110 0111010000001010 01100001 00100000 01101110 0110010101110111 00100000 01101110 01100001 0111010001101001 01101111 01101110 00101100 0010000001100011 01101111 01101110 01100011 0110010101101001 01110110 01100101 01100100 0010000001101001 01101110 00100000 01101100 0110100101100010 01100101 01110010 01110100 0111100100001010 01100001 01101110 01100100 0010000001100100 01100101 01100100 01101001 0110001101100001 01110100 01100101 01100100 0010000001110100 01101111 00100000 01110100 0110100001100101 00100000 01110000 01110010 0110111101110000 01101111 01110011 01110100 0110100101101111 01101110 00100000 01110100 0110100001100001 01110100 00001010 01100001 0110110001101100 00100000 01101101 01100101 0110111000100000 01100001 01110010 01100101 0010000001100011 01110010 01100101 01100001 0111010001100101 01100100 00100000 01100101 0111000101110101 01100001 01101100 00101110 00001010

Chapter 8 : Binary Data Files 5

Page 6: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Binary FileBinary 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 text files, everything has to be converted back and forth to binary, and this takes time.

Chapter 8 : Binary Data Files 6

Page 7: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Binary File Declared the FILE pointer exactly the same

way as a text file Used fopen and fclose

Second argument (wb, rb) – b for binary

Chapter 8 : Binary Data Files 7

Page 8: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Binary File : fwrite() fwrite moves (writes) the block of bytes from memory to

the file.

size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

Used fwrite with four arguments: ptr - Pointer to the array of elements to be written. size - Size in bytes of each element to be written. count - Number of elements, each one with a size of size bytes. stream - Pointer to a FILE object that specifies an output stream.

Chapter 8 : Binary Data Files 8

Page 9: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Binary File : Example 1 – creating even integers – fwrite()#include <stdio.h>#include <stdlib.h>

int main() { FILE *binaryp = fopen("nums.bin", "wb"); int i; for (i = 2; i < 500; i += 2) { fwrite(&i, sizeof(int), 1, binaryp); printf("%d\n", i); /* to print the written value */ } fclose(binaryp); return (EXIT_SUCCESS);}

Chapter 8 : Binary Data Files 9

Page 10: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Binary File : fread() Read block of data from stream.

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

Used fread with four arguments: ptr - Pointer to a block of memory with a minimum size of (size*count)

bytes. size - Size in bytes of each element to be read. count - Number of elements, each one with a size of size bytes. stream - Pointer to a FILE object that specifies an input stream.

Chapter 8 : Binary Data Files 10

Page 11: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Binary File : Example 1- fread()

Chapter 8 : Binary Data Files 11

#include <stdio.h>#include <stdlib.h>

int main() { FILE *binaryp = fopen("nums.bin", "rb"); int i, value; for (i = 2; !feof(binaryp); i += 2) { fread(&value, sizeof(int), 1, binaryp); printf("%d\n", value); /* to print the read value */ } fclose(binaryp); return (EXIT_SUCCESS);}

Page 12: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Test your own findings If a file was open in binary mode for write,

does the function fprintf() can be used to print something to the file?

If a file was open in binary mode for read, does the function fscanf() can be used to read something from the file?

The answer: Lets try at your computer at home and provide me the answer in the next class.

Chapter 8 : Binary Data Files 12

Page 13: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Chapter 8 : Binary Data Files 13

Summary

Page 14: Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8

Chapter 8 : Binary Data Files 14

BINARY FILES

•A sequence arbitrary bytes of structured data.

•Not in human readable form.

IMPORTANT!!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.