0

I'm using the following method to compare 2 files:

static bool comparatabelas(string path1, string path2)
    {
        byte[] file1 = File.ReadAllBytes(path1);
        byte[] file2 = File.ReadAllBytes(path2);
        if (file1.Length == file2.Length) //só verifica comprimento
        {
            //comando for adicionado ao original, com este dava sempre falso
            for (int i = 0; i < file1.Length; i++)
            {
                if (file1[i] != file2[i])
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

Comparing the files by iterating over every character in each of them works, and I have tried it several times:

for (int i = 0; i < file1.Length; i++)
{
    if (file1[i] != file2[i])
    {
        return false;
    }
}
return true;

However, if I use this particular piece of code it will give the result that the files are different even though they have the exact same content. I even tried to save the 2nd file in another folder but with the same name; however, I still got the result that they were different. Any advice on how I can fix this problem?

UPDATE:

I was saving tabledata's via xmlwrite, happens that when i was comparing, they had different table names, never tought it could be that, nevermind.

César Amorim
  • 367
  • 3
  • 16
  • When it returns false, what are the runtime values of `file1[i]` and `file2[i]`? How have you confirmed that the files are *exactly* the same? – David Apr 23 '14 at 00:40
  • What if you put breakpoint on `return false;` and see the exact values instead of guessing? – zerkms Apr 23 '14 at 00:40
  • obviously they are not the same.Also you can just use `SequenceEqual` instead of for loop. – Selman Genç Apr 23 '14 at 00:40
  • @David: doesn't `==` overload use `.Equals()` for strings? – zerkms Apr 23 '14 at 00:41
  • @zerkms: It indeed might, that wouldn't surprise me. But having more granular control over the comparison options could be of use. I just noticed, however, that these are byte arrays instead of strings, so it's kind of moot in this case anyway. – David Apr 23 '14 at 00:42
  • put a breakpoint on `return false` and see what it shows. – Dour High Arch Apr 23 '14 at 00:43
  • @zerkms i've just tried that, and they seem exactly the same – César Amorim Apr 23 '14 at 00:46
  • @César Amorim: so you're stating that C# returns `true` for `!=` comparison of 2 *equal* `byte`s? – zerkms Apr 23 '14 at 00:46
  • If `file1.Length < file2.Length`, then the character-by-character version will falsely report the files as equal. – Raymond Chen Apr 23 '14 at 00:47

0 Answers0