%% Move to New Position in File
% Open the following badpoem.txt file and perform read operations (which advance the position pointer) and then use seek to move to a new position in the file.
%
%
% Use fopen to open the file. Then, use ftell to query the current position.

fid = fopen('badpoem.txt');
ftell(fid)

% Read the first three lines and query the position in the file after each read. Use fgetl to read and fseek to examine the current position after the read operation.

tline1 = fgetl(fid)  % read the first line 
ftell(fid)

% Read the second line and examine the current position.

tline2 = fgetl(fid)  % read the second line 
ftell(fid)

% Read the third line and examine the current position.

tline3 = fgetl(fid)  % read the third line 
ftell(fid)

% To read line 2,  set the position in the file to point to the beginning of line 2. Use fseek to set the position, and then perform a read operation.

fseek(fid,20,'bof');
fgetl(fid)

% Close the file.

fclose(fid); 

% Copyright 2012 The MathWorks, Inc.