comparing two text files

Anonymous
timer Asked: Mar 9th, 2016

Question description

I have written the function to compare two text files as shown below

function [testStatus, testMessage] = compare2TxtFiles(File1, File2)

% DESCRIPTION

%   This functions compares two text file

%

% INPUTS:

% File1         - Name of the reference text file

% File2         - Name of the text file to be compared with the reference text file

%

% OUTPUTS:

% testMessage       - Prints unmatched results of the comparison

% testStatus        - Returns logical 1 if files matches and logical 0 otherwise

% Initialize outputs

testStatus = 1;

testMessage = {};

% Open, read each line (row) and close the first text file

% Opens the first (reference) text file File1 for reading

FID = fopen(File1, 'r'); 

% Reads File1 data into a cell array Data

Data = textscan(FID, '%s', 'delimiter', '\n', 'whitespace', '');  

% Returns the first line of cell array of strings Data to lines1

lines1 = Data{1};  

% Close the opened first file

fclose(FID);                                                                

% Open, read each line (row) and close the second text file

FID = fopen(File2, 'r');

Data = textscan(FID, '%s', 'delimiter', '\n', 'whitespace', '');

lines2 = Data{1};

fclose(FID);

% Get the number of elements in cellOneData1 and cellOneData2

% Number of elements in array lines1

numElOfLines1 = numel(lines1);

% Number of elements in array lines2

numElOfLines2 = numel(lines2);                              

% Compare the content the cell arrays lines1 and lines2

for i=1:numElOfLines1

    % If number of lines in lines1 are less than that of lines2

    if i <= numElOfLines2

        if strcmp(lines1{i},lines2{i})== 1

           

        else

            testStatus = 0;

            outText = ['Line ' num2str(i) ':' ' ' lines1{i} ' Is different from: ' lines2{i}];

            testMessage{end+1} = sprintf(outText);

        end

    else 

        % If number of lines in lines1 are more than that of lines2

        if i > numElOfLines2                    

            testStatus = 0;

            outText = 'Not Necessary to Compare the Files';

            testMessage{end+1} = sprintf(outText);

            return;

        end

    end 

end


But when i run the code, I do not get the expected results. The problem is that when I run the test, I get "Not Necessary to compare files" printed in the results of all the test cases.

Please can someone identify why i am having this problem and solve it?

Tutor Answer

(Top Tutor) Studypool Tutor
School: Carnegie Mellon University
Studypool has helped 1,244,100 students
flag Report DMCA
Similar Questions
Hot Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors