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 Files2
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 Files3
## 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 Files4
## binary file: ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## Chapter 7 : Binary Data Files5
## 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 Files6
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 Files7
Declared exactly the same way as a text file Used fopen and fclose ◦ Second argument (wb, rb) – b for binary Chapter 7 : Binary Data Files8
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 Files9
#include 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 Files10
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 Files11
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 Files12
#include 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 Files13
Chapter 7 : Binary Data Files14 Summary
Chapter 7 : Binary Data Files15 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.