I need to compare two txt files (file1 is used as baseline, and file2 is the one needs to compare with file1), I want to find out the differences to the file3, including missing lines, extra lines, and the lines with different content.
For my current code, if 2nd line in file2 is missing, all of following lines in file2 will be written into file3. How can skip the missing line in this case and only find out exactly different lines? Any ideas for this?
int file1LineNo = 0;
int file2LineNo = 0;
string file1lineStr;
string file2Str;
SortedDictionary<int, Object[]> info = new SortedDictionary<int, Object[]>();
string[] file1Lines = File.ReadAllLines(file1Name);
string[] file2Lines = File.ReadAllLines(file2Name);
while (file1LineNo<file1Lines.length)
{
file1lineStr = file1Lines[file1LineNo];
if (file1lineStr != null)
{
while(file2LineNo<file2Lines.Length)
{
file2Str = file2Lines[file2LineNo];
if (file1LineNo == file2LineNo)
{
if(!file2Str.Trim().Equals(file1Str.Trim()))
{
Result = false;
info.Add(rowNumber1++, new Object[]{"", file1lineStr, file2Str});
}
break;
}
file2LineNo++;
}
}
}
file1LineNo++;
}
foreach(var infoValue in info)
{
Object[] objectArr = info.Value;
for (int I=0; I<objectArr.Length; i++)
{
result.WriteResultToFile3(....);
}
rowed++;
}
return Result;
}
}
}