Random File Access CHAPTER 7.

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
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.
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.
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.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
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,
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
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.
 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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
Pascal Programming Today Chapter 11 1 Chapter 11.
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 Introduction Introduce some standard library functions.
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
1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs that read, write, and/or append binary files ❏ To be able.
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Characters and Strings
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
By C. Shing ITEC Dept Radford University
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
RECURSION Recursion is a process where a function calls itself. main()
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lecture 11 File input/output
TMF1414 Introduction to Programming
Introduction to Computer Programming Lecture 18 Binary Files
File I/O.
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
CS111 Computer Programming
Programming in C Input / Output.
What you need for the 1st phase of project
Binary Files.
Chapter 11 – File Processing
Lecture 13 Input/Output Files.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
C Programming Lecture-15 File I/O
Lecture 15 Files.
Text and Binary File Processing
File Input and Output.
Fundamental of Programming (C)
Programming in C Input / Output.
C Input / Output Prabhat Kumar Padhy
Files.
C Preprocessing File I/O
Chapter 12: File I/O.
Applications with Random File Access
A First Book of ANSI C Fourth Edition
ETE 132 Lecture 8 By Munirul Haque.
Professor Jodi Neely-Ritz University of Florida
Files Chapter 8.
Presentation transcript:

Random File Access CHAPTER 7

Text and Binary Files While the concepts of “text files” given, we processed files sequentially, one character, integer at a time. Each program read a character, then read the next character, then the next character, and so on. It is also possible to process the characters in a file randomly. Thus, a program can begin processing a file at any desired location within the file, then go to another location in the file, and so on at random.

Text and Binary Files A text file is a sequence of characters that occasionally includes a new-line character. In some operating systems, most notable DOS (also this is true for versions of the MS Windows), when a program places the new-line character into a file, the new-line character is physically stored two characters: Carriage Return (CR) Line Feed (LF) So, the new-line character is also named as CRLF mark. Similarly, when a program reads a CRLF pair, the system converts them to single new-line character (that are seen as a single character in the program).

Text and Binary Files In Unix, Linux like operating systems, the new-line character is indicated only with LF (line feed) character, and there is no need to do any conversion. When randomly processing a file, a program must know its exact position in the file. If we open the file in text file format, it is not possible to process file randomly. In text files, the new-line to CRLF conversions make it impossible to control the positioning in the file. It is a need to open the file in binary mode, in order to process it randomly.

Text and Binary Files When a program opens a file as a binary file, none of the characters is ever changed when read or written by the program. In a binary file the CRLF pair is not accepted as a single new-line character, instead, these are accepted as a pair of characters. Opening a file in binary mode is just needing to place the letter “b” at the end of the opening mode string in the fopen() statement.

Opening a Binary File To open a file in binary mode, use one of the following as the opening mode in fopen(). Mode Meaning . rb Read wb Write (Create) ab Append r+b or rb+ Read and write w+b or wb+ Write (Create) and read a+b or ab+ Append and read Example: input_file = fopen(“TEST.TXT”, “rb”); It opens the “TEST.TXT” file for reading in binary mode.

Opening a Binary File Open a file in binary mode, when it must appear to the program exactly as it is stored. That includes files that are to be : Processed randomly Executable files Data files (image files (like gif, jpg), sound file (like wav, mp3), movie files (avi, mpeg). In order to process a binary file randomly, you need to use the Random Access Functions.

Random Access Functions Random access functions are used to manipulate the file’s position indicator, or FPI. We will use getc(), and putc() functions for input/output purposes (to access the characters in a file). In order to access the character on the desired location really we need a special function to manipulate the FPI. The most useful direct access function in the C library is fseek().

Random Access Functions The syntax of the fseek() function is: fseek(file_pointer, offset, location) The third argument is one of three integer values. The third argument determines the value that the function places initially in the file’s FPI. The possible values of the third argument can be: Value Symbolic Equiv. Meaning . 0 SEEK_SET Sets the FPI to the first character in the file 1 SEEK_CUR Does not alter the FPI, stays at its current position. 2 SEEK_END Sets the FPI to the end of the file

Random Access Functions The second argument in fseek() is the offset, which must be a long integer. After setting the FPI by using the third argument, fseek() then adds the value of the second argument to the FPI. It carries the FPI to a position that is far the value of the second argument away from the main position given in the first argument. Examples: fopen(“TEST.TXT”,”rb”); FPI a b c d e f g h eof

Examples fseek(input_file,3L,SEEK_CUR); fseek(input_file,0L,SEEK_END); FPI 3 a b c d e f g h eof fseek(input_file,0L,SEEK_END); FPI 8 a b c d e f g h eof fseek(input_file,-2L,SEEK_CUR); FPI 6 a b c d e f g h eof

Examples fseek(input_file,0L,SEEK_SET); FPI a b c d e f g h eof fseek(input_file,-3L,SEEK_END); FPI 5 a b c d e f g h eof fseek(input_file,2L,SEEK_SET); FPI 2 a b c d e f g h eof

Examples fseek(input_file,8L,SEEK_SET); FPI 8 a b c d e f g h eof fseek(input_file,-8L,SEEK_END); FPI a b c d e f g h eof fseek(input_file,6L,SEEK_CUR); FPI 6 a b c d e f g h eof

Examples To detect the End-of-File position on a file, the feof() function can be used. Syntax : int feof( FILE *file-pointer); The function returns a nonzero value if the end of file has been detected. Ex.: while (!feof(file_ptr) { … }

Examples The READ AHEAD RULE: Read sequentially a file until you reach the end of file. ex.: FILE *input_file; char ch; input_file = fopen(“TEST.TXT”,”rb”); ch=getc(input_file); while (!feof(input_file)) { printf(“%c”,ch); } fclose(input_file);

Examples #include <stdio.h> #include <stdlib.h> #include <ctype.h> void main() { FILE *input_file; char ch; input_file= fopen(“EXAMPLE.TXT”,”rb+”); ch = getc(input_file); while (!feof(input_file) fseek(input_file, -1L, SEEK_CUR); putc(toupper(ch),input_file); fseek(input_file,0L, SEEK_CUR); } flose(input_file);

Examples Recall that, executing a getc() obtains a character and moves the FPI to the next character. Also, putc() moves the FPI to the next character after the character is printed. So, on the previous example, it was needed to bring the FPI to back to the current position with “-1L” offset. On rewrite operations, after the read operation, you have to bring the FPI on to the same position to rewrite the modified version of the read character.