1

Is there any way to compare two txt files starting from 2 line (or nth line)? I have tried to search online but unable to find any way

I have got many examples to compare two files but all examples are related to entire files.

Right now I am using following code

    private static bool FileCompare(string file1, string file2)
    {
        int file1byte;
        int file2byte;
        FileStream fs1;
        FileStream fs2;

        if (file1 == file2)
        {
            return true;
        }

        fs1 = new FileStream(file1, FileMode.Open, FileAccess.Read);
        fs2 = new FileStream(file2, FileMode.Open, FileAccess.Read);

        if (fs1.Length != fs2.Length)
        {
            fs1.Close();
            fs2.Close();

            return false;
        }

        do
        {
            file1byte = fs1.ReadByte();
            file2byte = fs2.ReadByte();
        }
        while ((file1byte == file2byte) && (file1byte != -1));

        fs1.Close();
        fs2.Close();

        return ((file1byte - file2byte) == 0);
    }
M005
  • 824
  • 1
  • 9
  • 25
  • 2
    Two loops, Linq, there are endless possibilities – maccettura Apr 26 '18 at 17:05
  • You haven't even tried to compare the lines in the code you posted. – nicomp Apr 26 '18 at 17:10
  • `file1 == file2` is not the smartest idea either, it's going to do a case-sensitive comparison. What if one path is lowercase and the other is uppercase? – s.m. Apr 26 '18 at 17:11
  • 1
    Also, your entire code could be replaced in 3 lines or so – maccettura Apr 26 '18 at 17:13
  • Sorry, I didn't understand how is your code relevant to what you want to achieve. Have you checked https://stackoverflow.com/questions/1262965/how-do-i-read-a-specified-line-in-a-text-file ? – AksharRoop Apr 26 '18 at 17:17

1 Answers1

1

The easiest way I can think to implement this is by using File.ReadLines() and some Linq:

private static bool FileCompare(string file1, string file2, int offset = 0)
{
    //Reads the lines, skips the "offset" number of lines
    var file1Lines = File.ReadLines(file1).Skip(offset);
    var file2Lines = File.ReadLines(file2).Skip(offset);

    //gets two collections of the differences, ignoring case
    var firstNotSecond = file1Lines.Except(file2Lines, StringComparer.OrdinalIgnoreCase);
    var secondNotFirst = file2Lines.Except(file1Lines, StringComparer.OrdinalIgnoreCase);

    //If there is nothing in both collections they are the same
    return !firstNotSecond.Any() && !secondNotFirst.Any();
}

Better Version Edit

I have learned something new today, @AleksAndreev pointed out in the comments that SequenceEqual() exists. My answer could be even more simplified:

private static bool FileCompareV2(string file1, string file2, int offset = 0)
{
    //Reads the lines, skips the "offset" number of lines
    var file1Lines = File.ReadLines(file1).Skip(offset);
    var file2Lines = File.ReadLines(file2).Skip(offset);

    return file1Lines.SequenceEqual(file2Lines, StringComparer.OrdinalIgnoreCase) && 
           file2Lines.SequenceEqual(file1Lines, StringComparer.OrdinalIgnoreCase);
}

Slightly modified fiddle for both solutions here

maccettura
  • 10,514
  • 3
  • 28
  • 35
  • 1
    Why not `file1Lines.SequenceEqual(file2Lines )`? – Aleks Andreev Apr 26 '18 at 18:22
  • @AleksAndreev I have edited my answer to include `SequenceEqual()` it was your suggestion however so if you decide to make your own answer I will remove that edit from my answer. Thanks for the knowledge bomb! – maccettura Apr 26 '18 at 18:29