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

Recommended Answers

All 2 Replies

Oh I have a perfect class for this, I have been meaning to make a CodeProject for this but I wrote this class that is designed to read in a file, and return a list with each new line as a new index in the list

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

//****************************************************************************
//* Responsible for reading in data from a file, and splitting data when
//* detecting a or newline (each of these increment the index of the list)
//****************************************************************************

namespace *INSERT NAMESPACE*
{
//==================================================================================================================
    class readInFile
    {
//-----------------------------------------------------------------------------------------------------------------
        public static List<string> readIn (string fileName)
        {
            List<string> input = new List<string>();

            try
            {
                if (File.Exists(fileName))
                {
                    string temp = File.ReadAllText(fileName);
                    input.AddRange(temp.Split(new String [] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)); //splits the data by new line
                }
                else
                {
                    MessageBox.Show("The File You Are Trying To Read In Does\nNot Exist Please Select An Exisiting FIle", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(error.ToString());
            }

            return input;
        }
//-----------------------------------------------------------------------------------------------------------------
    }
//==================================================================================================================
}

You call this class passing in the file name it needs to read from. Then it reads in the whole file and stores it in a string (temp). It then split that string up by new lines with each new line increment the list.

NOTE: This is written to be used for a Windows Form (you appear to be using a console). As such, you maybe have to change the MessageBox.Show() lines to Console.WritelLine() (or however you want to write to the console).

Now if you want to compare the number of lines you can do this

if(list1.Count() != list2.Count())
{
    //Message that the number of lines are different
}
else
{
    //Message that the number of lines are the same
}

Count() checks the number of indexes in the List

I hope this helps, and please don't hesistate to ask me questsions on this

I wrote this class that is designed to read in a file, and return a list with each new line as a new index in the list

You might want to consider these for your class:
Lines of text to an array
string[] TextLines = File.ReadAllLines(@"C:\Test.txt");
or if you want it in a list
List<string> TextLines = File.ReadAllLines(@"C:\Test.txt").ToList<string>();

Reading values from files, and comparing the two

I think you need something like this.

Load each file into its own list  'currently your're only loading one line
First make sure the files are the same length
Then use a For loop based on the .count of the the shorter one, if there is one
    and compare each list line by line, storing any different lines in a separate list
Now if the files were different lengths add the leftover lines from the longer one. 
Display the list of different lines
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.