简体   繁体   中英

Reading text from files, then storing and comparing them

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();
        }

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);

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:

在此处输入图片说明

Here is example solution (FileComparer.zip).

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM