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

 
 :)
:)
 :(
:(
 :))
:))
 :((
:((
 =))
=))
 =D>
=D>
 :D
:D
 :P
:P
 :-O
:-O
 :-?
:-?
 :-SS
:-SS
 :-f
:-f
 d(
d(
 :-*
:-*
 b-(
b-(
 h-(
h-(
 g-)
g-)
 5-p
5-p
 y-)
y-)
 c-)
c-)
 s-)
s-)
 d-)
d-)
 w-)
w-)
 :-h
:-h
 :X
:X



0 comments:
Enjoyed Reading? Share Your Views