2

I am comparing two files by reading them into filestream and comparing byte by byte. How can I skip whitespaces while comparing? I am using C#.net

linuxbuild
  • 15,843
  • 6
  • 60
  • 87
Thomas Manalil
  • 1,657
  • 4
  • 16
  • 23
  • Could you post your existing code and we can supply the changes? – MPritchard Sep 25 '09 at 11:05
  • If you didn't want to do the comparison yourself. You could use the FC.exe tool in windows, with the /W (compress/ignore whitespace) switch – Matt Lacey Sep 25 '09 at 11:26
  • In order to do this correctly you MUST know the encoding of the file. Is it plain 7 bit ASCII, UTF-8, UTF-16, encoded in some code page, what? – Eric Lippert Sep 25 '09 at 14:21
  • 2
    What I'm getting at is that basically, the operation you're performing here is logically inconsistent. You say you want to go byte by byte, but then do a character-by-character special case. That's logically inconsistent; bytes are not isomorphic to characters except in 7 bit ascii. Why are you not doing your entire comparison character-by-character? Perhaps you can describe what goal you have; there's probably a better way to achieve it than what you're trying to do. – Eric Lippert Sep 25 '09 at 14:24

1 Answers1

3
byte b;  

// ....

if (Char.IsWhiteSpace((char) b))
{
   // skip...
}

EDIT: As Eric Lippert points out, this is only correct if the encoding of the file is plain 7-bit ASCII. In any other encoding it will skip relevant bytes. So, you should take into account the encoding of your data.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 1
    This is only correct if the encoding of the file is plain 7 bit ASCII. In any other encoding it will skip relevant bytes. – Eric Lippert Sep 25 '09 at 14:20
  • @Eric Lippert: Hi, the poster said he was comparing bytes, but you are correct; he should be taking into account the encoding. – Mitch Wheat Sep 25 '09 at 14:33