1 / 21

Binary Files

Binary Files. Introduction. Each value is represented as a sequence of bytes. byte 0, byte 1, … The number of bytes and binary representation of values depends on the data type. The file position is at the first byte when the file in opened. Writing to a Binary File.

aine
Télécharger la présentation

Binary Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary Files

  2. Introduction • Each value is represented as a sequence of bytes. byte 0, byte 1, … • The number of bytes and binary representation of values depends on the data type. • The file position is at the first byte when the file in opened.

  3. Writing to a Binary File • The fwrite function is used to transfer a certain number of bytes beginning at a specified location in memory to a binary file. The data is written at the location in the file indicated by the file position pointer. • Example: int x=5, A[100]={…}; FILE* fptr=fopen(“numbers.bin”, “w”); fwrite(&x, sizeof(int),1,fptr); fwrite(A, sizeof(int),100,fptr);

  4. Reading from a Binary File • The fread function transfers a number of bytes from the location in the binary file indicated by the file position pointer to a certain memory location. • Example: int x, A[100]={…}; FILE* fptr=fopen(“numbers.bin”, “r”); fread(&x, sizeof(int),1,fptr); fraed(A, sizeof(int),100,fptr);

  5. Example: creating a simple binary file. #include <stdio.h> #include <iostream.h> void main(){ FILE * fptr=NULL; fptr=fopen(“c:\\students.bin”,”w”); if (fptr==NULL) cout<<”file could not be opened”; else{ int x; for(int I=1; I<=100; I++){ cin>>x; fwrite(&x,sizeof(int),1,fptr); }//for fclose(fptr); }//else }//main

  6. Example: reading a binary file #include <stdio.h> #include <iostream.h> void main(){ FILE * fptr=NULL; fptr=fopen(“c:\\students.bin”,”r”); if (fptr==NULL) cout<<”file could not be opened”; else{ int x; for(int I=1; I<=100; I++){ fread(&x,sizeof(int),1,fptr); cout>>x; } fclose(fptr); }//else }//main

  7. Example: writing data to a binary file. #include <stdio.h> #include <iostream.h> void main(){ struct student{char name[20]; int score}; student s; int i; FILE * fptr=NULL; fptr=fopen(“students.bin”, ”w”); if (fptr==NULL) cout<<”file could not be opened”; else{ for(i=1;i<=100;i++){ cin<<s.name<<s.score; fwrite(&s,sizeof(s),1,fptr); }//for fclose(fptr); }//else }//main

  8. Example: :reading data from binary file version. #include <stdio.h> #include <iostream.h> void main(){ struct student{char name[20]; int score}; student s; int i; FILE * fptr=NULL; fptr=fopen(“students.bin”, ”w”); if (fptr==NULL) cout<<”file could not be opened”; else{ while(!feof(fptr)){ fread(&s,sizeof(s),1,fptr); cout<<endl<<s.name<<s.score; }//while fclose(fptr); }//else }//main

  9. Exercises • write a program to find the student with the maximum score in the previously created file. • Write a program to increase the sores in the file by 5. You should copy the data into an array, update the array, and then write it back to the file.

  10. rewind Statement • The rewind statement is used to reset the file position to the beginning of the file. • Example: a program that displays scores for students given their names. This program keeps running until the user enters zzz. • Example: read a student name and display the rank.

  11. fseek function • The fseek is used to move the file position to a certain byte in the file. The general forms are: • fseek(fptr, I, SEEK_SET); sets the file position to the ith byte in the file (This byte will be read/written next) • fseek(fptr, I, SEEK_CUR); moves the file position forward I bytes (This byte will be read/written next) • fseek(fptr, I, SEEK_END); sets the file position to the ith byte from the end of the file (This byte will be read/written next)

  12. fseek • Assume the file position is currently J and the file contains n bytes numbered 0, 1, …, n-1. Then: fseek(fptr, I, SEEK_SET); // file position becomes I fseek(fptr, I, SEEK_CUR); // file position becomes J+I fseek(fptr, I, SEEK_END); // file position becomes n-I-1 • An example a binary file of 5 integers and a sequence of fseek, fread and cout statements.

  13. Direct Access of Records • Individual records are usually fixed in length. • Records are usually stored in an ascending of order of some key value. The offset of a record can then be calculated using: Key_value * record_size(

  14. Example A record of a bank client is defined as follows: struct bankClient{ int acctNum; char name[20]; double balance }; Write a program to create a binary file of 100 records each of which is defined as follows: bankClient emptyRecord={0, “”, 0};

  15. Example: write another program to read clients data from the key board and update the file created in the previous program. Account numbers should be 1-100. A record in which account number is I should be stored in the I’th record in the file. FILE *fptr; bankClient client; fptr=fopen(“credit.bin”,”r+”); // open the file for update; read and write cin>>clien.acctNum; cin>> clien.name cin>>clien.balance; fseek(fptr, (client. clien.acctNum-1)*sizeof(client), SEEK_SET); fwrite(&client, sizeof(client),1,fptr); fclose(fptr);

  16. Exercise Write a program to read from the KB a client number and find from the file from the previous example the name and balance of the client.

  17. Exercises • Write a function to count the number of bytes in a binary file. • Write a function to find the larges file among a list of files. char * longest(char filesNames[ ], int numOfFiles){…} • Write a function to combine a set of binary files into a new binary file. void combineFiles(char * filesNames[ ], int numOfFiles, char result[20]){…}

  18. Exercise Write a program to convert a binary file of students records into text file and vice versa.

  19. Exercise Given “students.bin”: • Write a function to find the number of records. int size(){…} • Write a function to return the score in a student record given the record number. int score(int rec){…} • Write a function to exchange two records given their numbers. int exchangeRecords(int i, int j){…} • Write a program to sort the file. Give 2 implementations; one does the sort on disk and the other in main memory. The first implementation can use the 3 preceding functions.

  20. Exercises • Write a program to merge 2 sorted binary files of integers into one sorted file. • Write a program to append a new record to the end of a binary file. You need to open the file in a mode, or open for r, reach eof then write, or use fseek(fptr,0,SEEK_END); • Write a program to delete a record from the binary file.

  21. Open file modes • r: open a file for reading • w: create a file for writing. If the file already exists, discard the current contents. • a: append; open or create a file for the writing at the end of the file. • r+: open a file for update(reading and writing). • w+: create a file for update. If the file already exists, discard the current contents. • a+: append; open or create a file for update; writing is done at the end of the file.

More Related