File Access Storage of data in variables and arrays is temporary—such data is lost when a program terminates. Files are used for permanent retention of.

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.
Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/ :30.
File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Lec11: File Processing 廖雪花 TEL: 年 5 月.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
The textread Function It is designed to read ASCII files that are formatted into columns of data Each column can be of a different type It is useful for.
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
1 Structure part 1 & File Processing. 2 Structures.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1 File Processing Dr. Serdar ÇELEBİ. 2 Outline Introduction The Data Hierarchy Files and Streams Creating a Sequential Access File Reading Data from a.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
CSC 211 Data Structures Lecture 32
1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes C++ errors/debugging build/compile compiler does not recognize a statement build/compile.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
 Pearson Education, Inc. All rights reserved Files and Streams.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
File Processing Part 2. Random Access File In sequential access file, record in a file created with the formatted output function fprintf are not necessarily.
© by Pearson Education, Inc. All Rights Reserved.
File Operations in Matlab Load / Save *.dat, *.mat vs. -ascii fopen /fclose.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
1 Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating a Sequential Access File 11.5Reading.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Chapter 11 – File Processing
11 C File Processing.
File Processing Part 2.
TMF1414 Introduction to Programming
EKT120: Computer Programming
Introduction to Computer Programming Lecture 18 Binary Files
Chapter 11 – File Processing
File Access (7.5) CSE 2031 Fall January 2019.
Fundamental of Programming (C)
File Access (7.5) CSE 2031 Fall February 2019.
Input/Output Functions
Pertemuan 14 File Processing II
EPSII 59:006 Spring 2004.
FILE handeling.
EPSII 59:006 Spring 2004.
EPSII 59:006 Spring 2004.
Input/Output Functions
Chapter 11 Files chap8.
Files Chapter 8.
Presentation transcript:

File Access Storage of data in variables and arrays is temporary—such data is lost when a program terminates. Files are used for permanent retention of data. In this chapter, we explain how data files are created, updated and processed by Matlab programs. We consider sequential-access files and random-access files. © by Pearson Education, Inc. All Rights Reserved.

Creating a File Programs may process no files, one file or several files. Each file used in a program must have a unique name and will have a different file id returned by fopen. All subsequent file processing functions after the file is opened must refer to the file with the appropriate file id. Files may be opened in one of several modes. To create a file, or to discard the contents of a file before writing data, open the file for writing ( "w" ). © by Pearson Education, Inc. All Rights Reserved.

Creating a File To read an existing file, open it for reading ( ‘r’ ). To open or create new file for writing (‘w’) To open or create new file, open the file for appending ( ‘a’ ). To open a file so that it may be written to and read from, open the file for updating in one of the three update modes— ’r+’, ‘w+’ or ‘a+’. Mode ‘r+’ opens a file for reading and writing. Mode ‘w+’ creates a file for reading and writing. If the file already exists, the file is opened and the current contents of the file are discarded. © by Pearson Education, Inc. All Rights Reserved.

Example – Sequential-access % Create the file fid = fopen(‘myfile.txt', 'w'); fwrite(fid, ‘hello’); fclose(fid); % Read the contents back into an array fid = fopen('myfile.txt'); str = fread(fid, [1, 5], 'int8=>char'); fclose(fid); © by Pearson Education, Inc. All Rights Reserved.

Creating a Sequential-Access File cfPtr = fopen( 'clients.txt','r'); if ( cfPtr == -1 ) fprintf( 'File could not be opened\n' ); else % display request options request = input( 'Enter request');

Creating a Sequential-Access File %process user's request while ( request ~= 4 ) switch ( request ) case 1 fprintf( '\nAccounts with zero balances:\n' ); %read file contents (until eof) while ( ~feof( cfPtr ) ) %read account, name and balance from file */ account = fscanf( cfPtr, '%d%s%f',[1 3]); if ( account (1,3) == 0 ) fprintf( '%-10d%-13s%7.2f\n',account); end

Creating a Sequential-Access File case 2 fprintf( '\nAccounts with - balances:\n' ); % read file contents (until eof) */ while ( ~feof( cfPtr ) ) %read account, name and balance from file */ account = fscanf( cfPtr, '%d%s%lf', [1 3]); if ( account(1,3) < 0 ) fprintf( '%-10d%-13s%7.2f\n',account ); end

Creating a Sequential-Access File case 3 fprintf( '\nAccounts with + balances:\n' ); % read file contents (until eof) */ while ( ~feof( cfPtr ) ) % read account, name and balance from file */ account = fscanf( cfPtr, '%d%s%lf',[1 3] ); if ( account(1,3) > 0 ) fprintf( '%-10d%-13s%7.2f\n',account ); end frewind( cfPtr ); % return cfPtr to beginning of file */ request = input('\n?'); end fprintf( 'End of run.\n' ); fclose( cfPtr ); % fclose closes the file */ end

Example – Sequential-access fid = fopen('bench.dat'); k = 0; while ~feof(fid) curr = fscanf(fid,'%c',1); if ~isempty(curr) k = k+1; benchstr(k) = curr; end fclose(fid); © by Pearson Education, Inc. All Rights Reserved.

Random-Access Files Records in a file created with the formatted output function fprintf are not necessarily the same length. However, individual records of a random-access file are normally fixed in length and may be accessed directly (and thus quickly) without searching through other records. This makes random-access files appropriate for airline reservation systems, banking systems, point-of-sale systems, and other kinds of transaction processing systems that require rapid access to specific data. © by Pearson Education, Inc. All Rights Reserved.

Example – Random-access fid1 = fopen('test1.dat', 'w+'); fwrite(fid1, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'); fid2 = fopen('test2.dat', 'w+'); fwrite(fid2, 'Second File'); % Seek to the 10th byte ('J'), read 5 fseek(fid1, 9, 'bof'); A = fread(fid1, 5, 'uint8=>char'); fclose(fid1); % Append to test2.dat fseek(fid2, 0, 'eof'); fwrite(fid2, A); fclose(fid2); © by Pearson Education, Inc. All Rights Reserved. 'bof' or -1Beginning of file 'cof' or 0Current position in file 'eof' or 1End of file

Example – Random-access cfPtr = fopen( 'clients.txt','r'); if ( cfPtr == -1 ) fprintf( 'File could not be opened\n' ); else % read record from file */ kayit = fscanf( cfPtr, '%d%s%lf', [3 3]); % update record */ fprintf( '%-10d%-13c%7.2f\n',kayit ); fclose(cfPtr); account = input( 'Enter account to update’); transaction=input('Enter charge(+)or payment(-):'); kayit(3,account) = kayit(3,account)+transaction; cfPtr = fopen( 'clients.txt','w+'); fprintf( cfPtr,'%-10d%-13c%7.2f\n',kayit ); fclose(cfPtr); end

Case Study: Transaction-Processing Program We now write a substantial transaction-processing program using random-access files. The program maintains a bank’s account information. The program updates existing accounts, adds new accounts, deletes accounts and prints a listing of all the current accounts.