Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Read a file in reverse -from end of file to Beginning of the file


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





like button Like "IMAGE PROCESSING" page

0 comments:

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Google ping Hypersmash.com