Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 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. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

3 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" ). ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

4 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. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

5 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); ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

6

7

8 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');

9 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

10 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

11 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

12 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); ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

13 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. ©1992-2010 by Pearson Education, Inc. All Rights Reserved.

14 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); ©1992-2010 by Pearson Education, Inc. All Rights Reserved. 'bof' or -1Beginning of file 'cof' or 0Current position in file 'eof' or 1End of file

15 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

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


Download ppt "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."

Similar presentations


Ads by Google