1 / 43

Introduction to Computer Programming Lecture 18 Binary Files

Introduction to Computer Programming Lecture 18 Binary Files. Assist.Prof. Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet . ozbek @ ege .edu.tr. RECALL: Text File Handling in C. Step 0 : Include stdio.h

gwakeman
Download Presentation

Introduction to Computer Programming Lecture 18 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. Introduction to Computer ProgrammingLecture 18 Binary Files • Assist.Prof.Dr. NükhetÖZBEK • Ege University • Department of Electrical&Electronics Engineering • nukhet.ozbek@ege.edu.tr

  2. RECALL: Text File Handling in C Step 0: Include stdio.h Step 1: Declare a file handler (a.k.a. file pointer) as FILE *for each file Step 2: Openfile using fopen() Step 3: Check if file is opened successfully Step 4a: Use fscanf() for input Step 4b: Use fprintf() for output Step 5: Closefile using fclose()

  3. Binary Files • When text files are used for storage of data, a program must spend extra effort to convert the stream of characters from an input file into integers and vice versa for output files • If the file is not to be read by a human, we can avoid this unnecessary translation

  4. Binary Files • A binary file is a file created by executing a program that stores directly in the file the computer’s internal representation of each file component

  5. Binary File Example FILE *binaryp; int i; binaryp = fopen(“nums.bin”, “wb”); for (i = 2; i <= 500; i+=2) fwrite(&i, sizeof(int), 1, binaryp); fclose(binaryp);

  6. Binary File Example Binary file declaration is the same as text file declaration FILE *binaryp; int i; binaryp = fopen(“nums.bin”, “wb”); for (i = 2; i <= 500; i+=2) fwrite(&i, sizeof(int), 1, binaryp); fclose(binaryp);

  7. Binary File Example ‘b’ character is used to denote that the file is a binary file FILE *binaryp; int i; binaryp = fopen(“nums.bin”, “wb”); for (i = 2; i <= 500; i+=2) fwrite(&i, sizeof(int), 1, binaryp); fclose(binaryp);

  8. Binary File Example FILE *binaryp; int i; binaryp = fopen(“nums.bin”, “wb”); for (i = 2; i <= 500; i+=2) fwrite(&i, sizeof(int), 1, binaryp); fclose(binaryp); fclose function is used just as for text files

  9. Binary File Example FILE *binaryp; int i; binaryp = fopen(“nums.bin”, “wb”); for (i = 2; i <= 500; i+=2) fwrite(&i, sizeof(int), 1, binaryp); fclose(binaryp); a different stdio library function is used

  10. fwrite function • fwrite function has 4 input parameters fwrite(&i, sizeof(int), 1, binaryp); Address of the first memory cell whose contents are to be copied to the file

  11. fwrite function • fwrite function has 4 input parameters fwrite(&i, sizeof(int), 1, binaryp); Number of bytes to copy to the file for one component

  12. sizeof operator • sizeof operator can be applied to any data type to find the number of bytes used for storage of the data type • sizeof operator can be applied to both built-in and user-defined types • E.g. • printf(“An integer requires %d bytes”,sizeof(int));

  13. fwrite function • fwrite function has 4 input parameters fwrite(&i, sizeof(int), 1, binaryp); Number of values to write to the file

  14. fwrite function • fwrite function has 4 input parameters fwrite(&i, sizeof(int), 1, binaryp); Pointer to the file which is opened for writing before

  15. Notes on fwrite • It is possible to write the contents of an entire array using just one fwrite call • E.g., • int score[10]; … fwrite(score, sizeof(int), 10, binaryp);

  16. fread function • fread function also requires four parameters • Address of the first memory cell to fill • Size of one value • Maximum number of elements to copy from the file into memory • File pointer to a binary file opened in mode “rb” • fread function returns an integer indicating how many elements it copied from the file

  17. Notes • It is important not to mix file types • A binary file created or written using fwrite must be read using fread • A text file created or written using fprintf must be read using fscanf

  18. Binary File Example /* FREAD.C: This program opens a file named FREAD.OUT and writes 25 characters to the file. It then tries to open the file and read in 25 characters. If the attempt succeeds, the program displays the number of actual items read. */ #include <stdio.h> int main( void ) { FILE *stream; int list[25]; int i, numread, numwritten; /* Open file in binary mode: */ stream = fopen( "fread.out", "wb" ); if( stream == NULL ) { printf( "Problem opening the file\n" ); return 1; }

  19. Binary File Example (cntd) for ( i = 0; i < 25; i++ ) list[i] = i; /* Write 25 characters to stream */ numwritten = fwrite( list, sizeof( int ), 25, stream ); printf( "Wrote %d items\n", numwritten ); fclose( stream ); stream = fopen( "fread.out", "rb" ); if( stream == NULL ) { printf( "File could not be opened\n" ); return 2; }

  20. Binary File Example (cntd) /* Attempt to read in 25 characters */ numread = fread( list, sizeof( int ), 25, stream ); printf( "Number of items read = %d\n", numread ); fclose( stream ); return 0; }

  21. Structures and Binary Files • Structures can also be written to and read from binary files using fwrite and fread functions • Structures are written or read as a “package” with one function • Structure arrays can also be written with one function

  22. Data I/O Using Text and Binary Files • Suppose we have a structure like the following: typedef struct { char name[10]; double diameter; int moons; … } planet_t;

  23. Data I/O Using Text and Binary Files (Opening for reading) • Text File I/O FILE *fp; fp = fopen(“planets.txt”,”r”); • Binary File I/O FILE *fp; fp = fopen(“planets.bin”,”rb”);

  24. Data I/O Using Text and Binary Files (Opening for writing) • Text File I/O FILE *fp; fp = fopen(“planets.txt”,”w”); • Binary File I/O FILE *fp; fp = fopen(“planets.bin”,”wb”);

  25. Data I/O Using Text and Binary Files (Read one structure from file) • Suppose: planet_t p; • Text File I/O fscanf(fp,”%s %lf %d”, p.name,&(p.diameter),&(p.moons)); • Binary File I/O fread(&p,sizeof(planet_t), 1, fp);

  26. Data I/O Using Text and Binary Files (Read N structure from file) • Suppose: planet_t p[N]; • Text File I/O for(i=0; i < N; i++) fscanf(fp,”%s %lf %d”, p[i].name,&(p[i].diameter),&(p[i].moons); • Binary File I/O fread(p,sizeof(planet_t), N, fp);

  27. Data I/O Using Text and Binary Files (Write one structure to file) • Suppose: planet_t p; • Text File I/O fprintf(fp,”%s %lf %d”, p.name,p.diameter,p.moons); • Binary File I/O fwrite(&p,sizeof(planet_t), 1, fp);

  28. Data I/O Using Text and Binary Files (Write N structure from file) • Suppose: planet_t p[N]; • Text File I/O for(i=0; i < N; i++) fprintf(fp,”%s %lf %d”,p[i].name,p[i].diameter,p[i].moons); • Binary File I/O fwrite(p,sizeof(planet_t), N, fp);

  29. Data I/O Using Text and Binary Files (Closing) • Text File I/O fclose(fp); • Binary File I/O fclose(fp);

  30. Two other file operations functions that may be useful • int remove(const char *filename); • Defined in <stdio.h> • Deletes the file specified by filename • int rename(const char *oldname, const char *newname); • Defined in <stdio.h> • Changes the name of a file from oldname to newname

  31. Disadvantages of Binary Files • A binary file created on one computer is rarely readable on another type of computer • Since a binary file can be read only by a specialized computer program, a person cannot proofread the file • A binary file cannot be created or modified in a word processor, so a program that expects binary file input cannot be tested until the program that produces the binary file is complete

  32. Random Files • Random file access refers to the ability to read or modify information directly at any given position in a file • This is done by getting and setting a file position indicator, which represents the current access position in the file associated with a given stream

  33. Random Access Files • Records in a file created with fprintf are not necessarily the same length • Individual records of a randomly accessed file are normally fixed in length and may be accessed directly (thus quickly) without searching through records • This makes random access files appropriate for systems that require rapid access to specific data

  34. 0 100 200 300 400 500 Random Access Files • Every record in a randomly accessed file normally has the same length • The exact location of a record relative to the beginning of the file can be calculated as a function of the record key 100 bytes 100 bytes 100 bytes 100 bytes 100 bytes etc. byte offsets

  35. Random Access Files • Normally, structs are used in random access files • Example program • A program to process bank accounts • Each record has an account number used as the record key, a last name, a first name and a balance • First a random access file of 100 records is created with 0 for account number, NULL for first name and last name and 0.0 for balance

  36. Example – creating a random accessed file (from Deitel) /* Creating a randomly accessed file sequentially */ #include <stdio.h> #define NRECORDS 100 struct clientData { int acctNum; char lastName[15]; char firstName[15]; double balance; };

  37. Example – creating a random accessed file (from Deitel) int main() { int i; struct clientData blankClient = {0, "", "", 0.0}; FILE *cfPtr; if ( (cfPtr = fopen("credit.dat", "wb")) == NULL) printf("File could not be opened.\n"); for (i = 1 ; i <= NRECORDS ; i++) fwrite(&blankClient, sizeof(struct clientData), 1, cfPtr); fclose(cfPtr); return 0; }

  38. Example – writing data randomly to a random accessed file (from Deitel) /* Writing data randomly to a random accessed file */ #include <stdio.h> #define NRECORDS 100 struct clientData { int acctNum; char lastName[15]; char firstName[15]; double balance; }; int main() { struct clientData client = {0, "", "", 0.0}; FILE *cfPtr;

  39. Example – writing data randomly to a random accessed file (from Deitel) if ( (cfPtr = fopen("credit.dat", "r+b")) == NULL) printf("File could not be opened.\n"); printf("Enter account number (1 to %d, 0 to end input)\n? ", NRECORDS); scanf("%d", &client.acctNum);

  40. Example – writing data randomly to a random accessed file (from Deitel) while(client.acctNum != 0) { printf("Enter lastname, firstname, balance\n? "); fscanf(stdin, "%s%s%lf", client.lastName, client.firstName, &client.balance); fseek(cfPtr, (client.acctNum - 1) * sizeof(struct clientData), SEEK_SET); fwrite(&client, sizeof(struct clientData), 1, cfPtr); printf("Enter account number (1 to %d, 0 to end input)\n? ", NRECORDS); scanf("%d", &client.acctNum); } fclose(cfPtr); return 0; }

  41. fseek function int fseek (FILE *stream, long int offset, int whence) • offset is the number of bytes from location whence in the file pointed to by stream • whence can have one of three values • SEEK_SET – seek starts at the beginning of the file • SEEK_CUR – seek starts at the current location in the file • SEEK_END – seek starts at the end of the file

  42. Example – reading data from a random accessed file (from Deitel) /* Reading data from a random accessed file */ #include <stdio.h> struct clientData { int acctNum; char lastName[15]; char firstName[15]; double balance; }; int main() { struct clientData client = {0, "", "", 0.0}; FILE *cfPtr;

  43. Example – reading data from a random accessed file (from Deitel) if ( (cfPtr = fopen("credit.dat", "rb")) == NULL) printf("File could not be opened.\n"); printf("%-6s%-16s%-11s%10s\n","Acct", "Last Name", "First Name", "Balance"); while(fread(&client, sizeof(struct clientData), 1, cfPtr)) if(client.acctNum != 0) printf("%-6d%-16s%-11s%10.2f\n", client.acctNum, client.lastName, client.firstName, client.balance); fclose(cfPtr); return 0; }

More Related