Download presentation
Presentation is loading. Please wait.
Published byHarriet Todd Modified over 9 years ago
1
Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8
2
Chapter 8 : Binary Data Files2 Objectives: You’ll learn about; Introduction Fundamentals of binary data files Processing binary files Files with mixed type data Related example
3
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 Files3
4
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 Files4
5
Content inside Binary File: Example (con’t) 01000110 01101111 010101 01110010 00100000 01110011 01100011 01101111 01110010 01100101 00100000 0111100001 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 8 : Binary Data Files5
6
Binary File 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 text files, everything has to be converted back and forth to binary, and this takes time. Chapter 8 : Binary Data Files6
7
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 Files7
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 Files8
9
Binary File : Example 1 – creating even integers – fwrite() #include 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 Files9
10
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 Files10
11
Binary File : Example 1- fread() Chapter 8 : Binary Data Files11 #include 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); }
12
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 Files12
13
Chapter 8 : Binary Data Files13 Summary
14
Chapter 8 : Binary Data Files14 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.