1 / 13

Applications with Random File Access

Applications with Random File Access. CHAPTER 8. Where am I on the Binary File?. While a binary file is processed, it is a need to know current position, really which byte is being processed. The FPI is containing the address of the current byte in the file.

cormac
Download Presentation

Applications with Random File Access

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. Applications with Random File Access CHAPTER 8

  2. Where am I on the Binary File? • While a binary file is processed, it is a need to know current position, really which byte is being processed. • The FPI is containing the address of the current byte in the file. • ftell() is the function that can be used to learn the content of the FPI. The syntax of the ftell() is: long ftell(file_pointer); • So, the ftell() function is returning the file position of the current byte in LONG data type.

  3. The ftell() Function Example: #include <stdio.h> void main() { FILE *fp; long position; fp = fopen(“TEST.TXT”,”rb”); position = ftell(fp); printf(“\n Now, we are on the FIRST byte, FPI=%ld”, position); fseek(fp,0L,SEEK_END); position = ftell(fp); printf(“\n Now, we are on the LAST byte, FPI=%ld”, position); fseek(fp,10L,SEEK_SET); position = ftell(fp); printf(“\n Now, we are on the byte 10 from begining, FPI=%ld”, position); fseek(fp,-10L,SEEK_END); position = ftell(fp); printf(“\n Now, we are on the byte 10 from end, FPI=%ld”, position); fclose(fp); }

  4. Example 1 • Problem 1: Write a program to create a binary file called “TEST.TXT”, and fill it with 300 randomly selected small case letters. Hint: It was seen that, to generate a random number between 5 to 60, the formula was: 5 + rand() % ( 60 – 5 +1); Therefore, the formula for random letters between ‘a’ to ‘z’ should be: ‘a’ + rand() % ( ‘z’ - ’a’ +1);

  5. Example 1 • Solution: #include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { FILE *fp; int i; randomize(); fp = fopen(“TEST.TXT","wb"); for (i=0; i<300; i++) putc('a'+rand()%('z'-'a'+1), fp); fclose(fp); }

  6. Example 2 • Problem 2: Write a program to find the length (size in number of bytes) and display of the binary file “TEST.TXT”. • Use a method that is not needing any sequential read or any loop! HINT: Use fseek() to go to the end of file, and then use the ftell() to learn the FPI content.

  7. Example 2 • Solution: #include <stdio.h> #include <conio.h> void main() { FILE *fp; long j=0; fp = fopen(“TEST.TXT","rb"); fseek(fp,0L, SEEK_END); j=ftell(fp); printf("\n The number of bytes = %ld", j); getch(); }

  8. Example 3 • Problem 3: Write a program to read the binary file “TEST.TXT” to do the followings: • List the all content (sequential read). • List only the letters, which are on the odd numbered positions within the file. • List only the letters, which are on the even numbered positions within the file. HINT: Use fseek() and modify “offset” argument to move two by two on the file.

  9. Example 3 • Solution: #include <stdio.h> #include <conio.h> void main() { FILE *fp; int i; char ch; long j=0; fp = fopen(“TEST.TXT","rb"); printf("\n The all :\n"); while (!feof(fp)) { ch=getc(fp); printf("%c",ch); }

  10. Example 3 • Solution: (continous) printf("\n\n\n The Odd Positioned characters :\n"); for (i=0; i<150; i++) { j=2*i+1; fseek(fp, j, SEEK_SET); ch=getc(fp); printf("%c",ch); } printf("\n\n\n The Even Positioned characters :\n"); for (i=0; i<150; i++) { j=2*i; fseek(fp, j, SEEK_SET); ch=getc(fp); printf("%c",ch); } fclose(fp); getch(); }

  11. Example 4 • Problem 4: Write a program to read the binary file “TEST.TXT” in REVERSE order and write into another binary file “REVERSE.TXT”. HINT: a) Use fseek() with SEEK_END value to find the last letter of the file. b) Use a loop and check the FPI with ftell() to know if it is still greater then zero (Is it reached to the position of the first letter of the file). c) On every turn of the loop, after getc() (reading a letter), use fseek with –2L offset to position the FPI to the previous of the current letter (in order to process file in reverse order).

  12. Example 4 • Solution: #include <stdio.h> #include <conio.h> void main() { FILE *fp_in, *fp_out; long j; char ch; fp_in = fopen("DENEME.TXT","rb"); fp_out = fopen("REVERSE.TXT","wb"); fseek(fp_in,-1L, SEEK_END);

  13. Example 4 • Solution: (Continous) j=ftell(fp_in); /* Position of the first letter of the file */ while (j>0) /* Is the current position the fist letter’s position ?*/ { ch=getc(fp_in); /* getch() automatically advancing the FPI to next */ fseek(fp_in,-2L, SEEK_CUR); /* Set the FPI to the previous position*/ putc(ch,fp_out); j=ftell(fp_in); /* Set the current position into the variable J*/ printf("\n %ld",j); } ch=getc(fp_in); /* Read the first letter of the file*/ putc(ch,fp_out); fclose(fp_in); fclose(fp_out); getch(); }

More Related