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,

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.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
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.
Programming In C++ Spring Semester 2013 Lecture 10 Programming In C++, Lecture 10 By Umer Rana.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Introduction to C Programming
Python Mini-Course University of Oklahoma Department of Psychology Lesson 17 Reading and Writing Files 5/10/09 Python Mini-Course: Lesson 17 1.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
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.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
 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.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
File IO and command line input CSE 2451 Rong Shi.
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.
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()”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Characters and Strings
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
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.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
 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.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Lecture 11 File input/output
TMF1414 Introduction to Programming
File I/O.
CS111 Computer Programming
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
Topics Introduction to File Input and Output
C Programming Lecture-15 File I/O
Random File Access CHAPTER 7.
Text and Binary File Processing
File Input and Output.
Fundamental of Programming (C)
Topics Introduction to File Input and Output
C Preprocessing File I/O
File Handling in C.
Chapter 12: File I/O.
Applications with Random File Access
Topics Introduction to File Input and Output
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

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, 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.

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

C.12 4 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.

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

C.12 6 Opening a Binary File » To open a file in binary mode, use one of the following as the opening mode in fopen(). ModeMeaning. rbRead wbWrite (Create) abAppend 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.

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

C.12 8 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().

C.12 9 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: ValueSymbolic Equiv. Meaning. 0SEEK_SET Sets the FPI to the first character in the file 1 SEEK_CUR Does not alter the FPI, stays at its current position. 2SEEK_ENDSets the FPI to the end of the file

C 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”); 0 0 a a b b c c d d e e f f g g h h eof FPI

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

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

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

C 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) { … }

C 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); ch=getc(input_file); } fclose(input_file);

C Examples #include 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); // why?? because of a bug! ch = getc(input_file); } fclose(input_file); }

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