file access storage of data in variables and arrays is temporary—such data is lost when a program...

16

Upload: melissa-todd

Post on 18-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: 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
Page 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

File AccessStorage 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.

Page 3: 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

Creating a FilePrograms 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.

Page 4: 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

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.

Page 5: 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

Example – Sequential-access% Create the filefid = fopen(‘myfile.txt', 'w');fwrite(fid, ‘hello’);fclose(fid);

% Read the contents back into an arrayfid = fopen('myfile.txt');str = fread(fid, [1, 5], 'int8=>char');fclose(fid);

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

Page 6: 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

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

Page 7: 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

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

Page 8: 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

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

Page 9: 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

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 end

Page 10: 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

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 end

Page 11: 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

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 end 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

Page 12: 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

Example – Sequential-accessfid = fopen('bench.dat');

k = 0;while ~feof(fid) curr = fscanf(fid,'%c',1); if ~isempty(curr) k = k+1; benchstr(k) = curr; endend fclose(fid);

©1992-2010 by Pearson Education, Inc. All Rights Reserved.

Page 13: 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

Random-Access FilesRecords 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.

Page 14: 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

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 -1 Beginning of file'cof' or 0 Current position

in file'eof' or 1 End of file

Page 15: 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

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

Page 16: 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

Case Study: Transaction-Processing ProgramWe 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.