Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8
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
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
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
Content inside Binary File: Example (con’t) Chapter 8 : Binary Data Files5
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
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
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
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
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
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); }
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
Chapter 8 : Binary Data Files13 Summary
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.