Chapter8-2i - Chapter8Part2 OtherFileOperations...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
EGR 115 - Prof Newman            Chapter 8 - Part 2            Other File Operations  Reading Single Lines of Data File Status File Locating and Naming Assists
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
EGR 115 - Prof Newman fgetl - reads the next line of data excluding end-of-line characters Format: array = fgetl(fid) fgets - reads the next line of data including end-of-line characters Format: array = fgets(fid) If either encounter end-of-file, returns -1.                        Reading Single Lines of Data
Background image of page 2
EGR 115 - Prof Newman Suppose x.txt holds the data Hello – My name is Sam\n And my friend’s name is Jane!\n Let’s read with fgets and fgetl             Reading Single Lines of Data 22 + 1 Chars 29 + 1 Chars
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
EGR 115 - Prof Newman % Program x1.m % Open the file fid=fopen( 'x.txt' , 'r' ); % Read the file z1 = fgets(fid); z2 = fgetl(fid); z3 = fgetl(fid); % already at EOF % Close the file fclose(fid); % Show the data items read disp(z1) fprintf( 'Length of z1 = %g\n' ,length(z1)) disp(z2) fprintf( 'Length of z2 = %g\n' ,length(z2)) disp(z3) fprintf( 'Length of z3 = %g\n' ,length(z3))             Reading Single Lines of Data
Background image of page 4
EGR 115 - Prof Newman % Program x1.m disp(z1) fprintf( disp(z2) fprintf( disp(z3) fprintf( 'Length of z3 = %g\n' ,length(z3))             Reading Single Lines of Data Hello – My name is Sam\n Length of z1 = 23 And my friend’s name is Jane! Length of z2 = 29 -1 Length of z3 = 1
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
EGR 115 - Prof Newman             File Positioning & Status exist - checks for the existence of a file, variable, function, or directory. status = exist(‘filename’) returns status depending on file existence where status > 0 if file exists and is defined as: 2 if filename is an M-file on MATLAB's search path. It also returns 2 when filename is the full pathname to a file or when filename is the name of an ordinary file on MATLAB's search path Useful to check if file exists before opening with ‘w’ to determine if should be overwritten, or prevent an error if attempting to read a non- existing file before opening with a ‘r’.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/13/2010 for the course EGR EGR115 taught by Professor Newman during the Fall '10 term at Embry-Riddle FL/AZ.

Page1 / 23

Chapter8-2i - Chapter8Part2 OtherFileOperations...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online