0

I'm attempting to create a program in C# that reads lines of text from a text file and stores them in a list. Then I have to compare each line to another equally large (50 lines) text file, and display the differences to the screen? Could anyone help? It would be appreciated. So far I've only been able to read the files.

    TextReader tr = new StreamReader("file1.txt");
        for (var i = 0; i < 1; i++)
        {
            tr.ReadLine();
        }
    TextReader tra = new StreamReader("file2.txt");
        for (var f = 0; f < 1; f++)
        {
            tra.ReadLine();
        }
8
  • 1
    Finding the differences between to texts is a very difficult task and highly depends on the context. Can you precise what kind of content you want to compare (program, numbers, ...)? Jan 23, 2013 at 21:31
  • 1
    you could load all of the files into 2 separate List<string> and then iterate the list based on the contents of the other list ..there are many options you have here.. it would also help if you posted a small snippet of what the data that's being compared looks like as well
    – MethodMan
    Jan 23, 2013 at 21:32
  • Is the comparison you need to do just line-by-line, or do you have to take into account differences that span multiple lines?
    – Melanie
    Jan 23, 2013 at 21:32
  • Sorry guys. The two files mentioned are being compared line-by-line. On each line, theres only one character(quiz answers in one file, answer key on the other. Im essentially creating a program that will grade the quiz using another file that serves as an answer key. Jan 23, 2013 at 21:37
  • In that case, I vote for DJ KRAZE's solution: List<string>
    – Melanie
    Jan 23, 2013 at 21:40

3 Answers 3

1

theres only one character(quiz answers in one file, answer key on the other

var count = File.ReadLines("file1.txt")
                 .Zip(File.ReadLines("file2.txt"), (f1, f2) => f1 == f2)
                 .Count(b => b);

INPUT: file1.txt

a
a
c
d

INPUT: file2.txt

a
a
b
d

OUTPUT:

3


EDIT for @AlexeiLevenkov

var two = new[] { true, false }.Count();
var one = new[] { true, false }.Count(b => b);
2
  • 1
    @AlexeiLevenkov In fact I need that lambda, see the edit and test it.
    – I4V
    Jan 23, 2013 at 22:23
  • 1
    +1. My previous comment about no need for lambda in Count was wrong - the sequence contains isMathing value, so you only need to count once that are isMatching == true. Jan 23, 2013 at 23:01
0

You can create simple class to hold necessary data. In this class we store lines from each file and Color to indicate equality or not.

public class LineComparer
{
        public string Line1 { get; set; }
        public string Line2 { get; set; }
        public Brush Color { get; set; }
}

In the next step you must populate list with data from files:

public List<LineComparer> _comparer = new List<LineComparer>();

public void ReadFiles()
{
    TextReader tr1 = new StreamReader("file1.txt");
    TextReader tr2 = new StreamReader("file2.txt");

    string line1, line2 = null;

    while ((line1 = tr1.ReadLine()) != null)
    {
        _comparer.Add(new LineComparer{ Line1 = line1 });
    }

    int index = 0;

    while ((line2 = tr2.ReadLine()) != null)
    {
        if(index < _comparer.Count)
            _comparer[index].Line2 = line2;
        else
            _comparer.Add(new LineComparer{ Line2 = line2 });
        index++;
    }

    tr1.Close();
    tr2.Close();

    _comparer.ForEach(x => { if(x.Line1 != x.Line2) x.Color = new SolidColorBrush(Colors.Red); else x.Color = new SolidColorBrush(Colors.Green); });
}

To present files differences you can use ListBox with ItemTemplate:

<ListBox ItemsSource="{Binding}"
         Grid.IsSharedSizeScope="True"
         >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Background="{Binding Color}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" SharedSizeGroup="A" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="*" SharedSizeGroup="B" />
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding Line1}" 
                           TextWrapping="Wrap" />

                <TextBlock Text="{Binding Line2}" 
                           TextWrapping="Wrap"
                           Grid.Column="2"
                           />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Example:

"file1.txt":

First
Second
Third
Fourth
Fifth
Sixth
Seventh

"file2.txt":

First
second
Third
Fourth
Fifth

and the result is:

enter image description here

Here is example solution (FileComparer.zip).

2
  • Your answer doesn't work, at least for color blinds like me :)))
    – I4V
    Jan 23, 2013 at 22:39
  • @I4V check my answer :) I added a link to sample solution.
    – kmatyaszek
    Jan 24, 2013 at 16:06
0
List<string> testlist1 = new List<string>();
List<string> testlist2 = new List<string>();
//populate Lists
for (int i = 0; i < testlist1.Count; i++)
{
     if (testlist2[i] == testlist1[i])
          //do something
     else
         //do something else
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.