简体   繁体   English

从文件中读取文本,然后进行存储和比较

[英]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. 我试图用C#创建一个程序,该程序从文本文件中读取文本行并将它们存储在列表中。 Then I have to compare each line to another equally large (50 lines) text file, and display the differences to the screen? 然后,我必须将每一行与另一个同样大的文本行(50行)进行比较,然后将差异显示在屏幕上吗? 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 输入:file1.txt

a
a
c
d

INPUT: file2.txt 输入:file2.txt

a
a
b
d

OUTPUT: 输出:

3 3


EDIT for @AlexeiLevenkov 编辑@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. 在此类中,我们存储每个文件中的行和Color来指示是否相等。

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 : 要显示文件差异,可以将ListBoxItemTemplate使用:

<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": “ file1.txt”:

First
Second
Third
Fourth
Fifth
Sixth
Seventh

"file2.txt": “ file2.txt”:

First
second
Third
Fourth
Fifth

and the result is: 结果是:

在此处输入图片说明

Here is example solution (FileComparer.zip). 是示例解决方案(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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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