Applications with Random File Access CHAPTER 8. C.13 1 Where am I on the Binary File? » While a binary file is processed, it is a need to know current.

Slides:



Advertisements
Similar presentations
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Advertisements

Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Formatted Files.
FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
OS Project #2:Random File Access  Random file access: using the lseek() system call to access files in UNIX. zTake arguments from the command line. 
CS1061: C Programming Lecture 19: Random Access Files A. O’Riordan, 2004, 2007 updated.
Structures and Unions Chapter 6. Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
 Types of files  Command line arguments  File input and output functions  Binary files  Random access.
Random File Access CHAPTER 7. C.12 2 Text and Binary Files » While the concepts of “text files” given, we processed files sequentially, one character,
N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Winter 2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Assignment-II Tips.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
PHP Programming.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 7 Files By C. Shing ITEC Dept Radford University.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Introduction to Programming
C Programming Lecture 12 : File Processing
Computer Programming for Engineers
‘ typedef ’ is a keyword,which allows you to specify a new name for a datatype which is already defined in c language program. Syntax: typedef /* Re-defining.
Adv. UNIX:fp/101 Advanced UNIX v Objectives of these slides: –a more detailed look at file processing in C Special Topics in Comp. Eng.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
11 C File Processing.
Lecture 12 CIS 208 Friday, March 3rd , 2005.
External Files: Abstractly, a file can be thought of as a stream of data (either char or binary). C has two groups of files: standard files, such as stdin,
Chapter 4 File Processing
Lecture 11 File input/output
TMF1414 Introduction to Programming
Introduction to Computer Programming Lecture 18 Binary Files
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
File Handling in C.
CS111 Computer Programming
What you need for the 1st phase of project
File Handling in C.
FILE HANDLING.
Introduction to Programming
Introduction to Programming
Programming and Data Structures
Chapter 11 – File Processing
Lecture 15 Files.
תכנות מערכות בשפת C עבודה עם קבצים מכללת אורט כפר-סבא אורי וולטמן
FILE HANDLING IN C.
Random File Access CHAPTER 7.
Text and Binary File Processing
File Handling in C.
Henning Schulzrinne Columbia University
Fundamental of Programming (C)
C Input / Output Prabhat Kumar Padhy
Chapter 12: File I/O.
FILE handeling.
Applications with Random File Access
EECE.2160 ECE Application Programming
Professor Jodi Neely-Ritz University of Florida
Programming Fundamental
Presentation transcript:

Applications with Random File Access CHAPTER 8

C.13 1 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.

C.13 2 The ftell() Function Example: #include 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); }

C.13 3 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);

C.13 4 Example 1 » Solution: #include 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); }

C.13 5 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.

C.13 6 Example 2 » Solution: #include 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(); }

C.13 7 Example 3 » Problem 3: Write a program to read the binary file “TEST.TXT” to do the followings: a) List the all content (sequential read). b) List only the letters, which are on the odd numbered positions within the file. c) 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.

C.13 8 Example 3 » Solution: #include 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); }

C.13 9 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(); }

C 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).

C Example 4 » Solution: #include 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);

C 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(); }