iphone has asked for the wisdom of the Perl Monks concerning the following question:

Given two files I just need to know whether they are different or not,does a string comparison or any generic diff tool will work?Basically it should also ignore comments.Has anybody already tried such a tool?Any suggestions or pointers on how to achieve this is appreciated

  • Comment on How to know if two files (.c,.h,.cpp) are different content-wise ignoring the comments?

Replies are listed 'Best First'.
Re: How to know if two files (.c,.h,.cpp) are different content-wise ignoring the comments?
by elef (Friar) on Feb 13, 2011 at 08:45 UTC
    Another idea would be to run both files through B::Deparse.
    That would standardise whitespace, get rid of comments and standardise code structures, too. Perhaps overkill, but it's probably the only way to tell if two files are compiled identically.

    Edit: I just saw you mention .cpp, i.e. this is not about perl code, so B::Deparse won't work. The problem I can see with just stripping comments and comparing the files is that even if you strip empty lines, they may have different whitespace within lines that's not relevant to the code but would be picked up by tools like diff.
Re: How to know if two files (.c,.h,.cpp) are different content-wise ignoring the comments? (perl)
by tye (Sage) on Feb 13, 2011 at 17:52 UTC

    The bottom half of Re: C comment stripping preprocessor (problems) contains a Perl script that strips C-style and C++-style comments from C and C++ code. Note that you should initialize $canNest= 0 as C/C++ don't allow nesting of C-style comments. You might also want to manipulate whitespace so you can ignore difference in whitespace only outside of quoted strings.

    - tye        

Re: How to know if two files (.c,.h,.cpp) are different content-wise ignoring the comments?
by Anonymous Monk on Feb 13, 2011 at 06:01 UTC
    1. strip comments
    2. step two, diff

      how can a tool diff and figure out if they are different or not after stripping the comments?