Consider a file that contains the following text,
Brand New World
After reading the file in the reverse order the
result will be
dlroW weN dnarB
MATLAB
CODE:
%Open a text
file
fp=fopen('rev.txt');
%Move to the
End Of the File
fseek(fp,0,'eof');
%Get the
position
fsz=ftell(fp);
%Preallocate
the character matrix
M=char(zeros([1
fsz]));
n=1;
%Move one
position backward
fseek(fp,-1,'cof');
Here ‘cof’ is Current
position of the file
%Read the
character
c=fread(fp,1,'*char');
Here the ‘fread’
function reads one data in character format
%Store the
character in the matrix
M(1,n)=c;
%Check whether
the file pointer has reached the beginning of the file
while(fseek(fp,-2,'cof')~=-1)
n=n+1;
c=fread(fp,1,'*char');
M(1,n)=c;
end
fclose(fp);
display(M);
Result:
M =
dlroW weN dnarB
No comments:
Post a Comment