hi there, im very much new in programming, and i'm having a hard time in comparing two datafiles using c#, when im adding the while loop it doesnt work, but when i remove it, it compares the first line of my datafile, what i want to do it to compare my datafile line by line... heres my code, hope you could help me on this one, thank you in advance:)...

private void match_Click(object sender, EventArgs e)
        {
            //string f1, f2;

            try
            {                
                string fileName = @"C:\PRJ2000_LFSDATA\file1.DAT";
                string filename2 = @"C:\PRJ2000_LFSDATA\file2.DAT";
                StreamReader sr = new StreamReader(fileName);
                StreamReader sr2 = new StreamReader(filename2);
               
                StreamWriter SW = File.CreateText("c:\\temp\\MyTextFile.txt");

               
                while ((!sr.EndOfStream) && (!sr2.EndOfStream))
                {
                    if ((x.Substring(12, 2)) == (x2.Substring(12, 2))) //PRV
                    {
                        if ((x.Substring(14, 2)) == (x2.Substring(14, 2)))//MUN
                        {
                            if ((x.Substring(16, 3)) == (x2.Substring(16, 3)))//BGY
                            {
                                if ((x.Substring(19, 3)) == (x2.Substring(19, 3)))//EA
                                {
                                    if ((x.Substring(27, 4)) == (x2.Substring(27, 4)))//SHSN
                                    {
                                        if ((x.Substring(31, 4)) == (x2.Substring(31, 4)))//HCN
                                        {
                                            //SW.Write("HCN  Equal" + "\t\nf1= " + sr.ReadLine().Substring(1, 21) + sr.ReadLine().Substring(27, 4) +
                                            //                        "\t\nf2= " + sr2.ReadLine().Substring(1, 21) + sr2.ReadLine().Substring(27, 4));
                                            //SW.Write("\n");
                                            //SW.Close();
                                        }
                                        else
                                        {

                                            SW.Write("HCN not Equal" + "\t\nf1= " + sr.ReadLine().Substring(1, 35) + " " + sr.ReadLine().Substring(27, 4) +
                                                                       "\t\nf2= " + sr2.ReadLine().Substring(1, 35) + " " + sr2.ReadLine().Substring(27, 4));
                                            SW.Write("\n");
                                            SW.Close();

                                        }
                                    }
                                    else
                                    {


                                        SW.Write("SHSN not Equal" + "\r\n f1= " + sr.ReadLine().Substring(1, 35) + " " + sr.ReadLine().Substring(27, 4) +
                                                                    "\r\n f2= " + sr2.ReadLine().Substring(1, 35) + " " + sr2.ReadLine().Substring(27, 4));
                                        SW.Write("\n");
                                        SW.Close();

                                    }
                                }
                            }
                        }
                    }
                }
                                 
                System.Diagnostics.Process.Start(@"C:\temp\MyTextFile.txt");   
                              
                   //MessageBox.Show("database READ");
                   sr.Close();
                   sr2.Close();
            }

            catch
            {
                MessageBox.Show("error Running");
            }
        }

You're comparing various parts of each line to each other (it appears), but if you want to see if the entire lines are equal, why not just compare the entire lines?

A quick way to achieve this would be to use a static method of the type File to read all lines of each file into arrays of strings. File.ReadAllLines(fileName) From there, you can quickly see if the files are different by first comparing the lengths of the arrays. If they're not equal, you know immediately the files do not match. Then you can iterate over the arrays and compare them line by line.

thank you for the reply apegram, but i do really need to compare my datafiles line by line, there are really times that the length of both datafiles are not the same, my plan here is to show in notepad the lines that doesnt have a match, please help me again

What apegram has suggested will allow you to compare line by line. In your example you are comparing x with x2 but i dont see where you have declared them and you aren't populating them.
You either need to populate them with the next line from the streamreader x = sr.ReadLine() or load all the lines into an array (as apegram said) and then use a foreach loop to iterate through the array elements.

There is, however, a flaw in your logic: If you were to take a valid file (file1), delete the first line and save it (file2), then compare the two files; your program would consider every line to be incorrect. Is this intentional? Will your lines always be in the correct order, and will lines ever be omitted from a file?
Likewise, if file1 is longer than file2, your While loop will exit when one file is finished and the remaining lines in the longer file wont be checked or output.

EDIT: also, i think what apegram was trying to point out about your substring comparisons is that

if ((x.Substring(12, 2)) == (x2.Substring(12, 2))) //PRV
                    {
                        if ((x.Substring(14, 2)) == (x2.Substring(14, 2)))//MUN
                        {
                            if ((x.Substring(16, 3)) == (x2.Substring(16, 3)))//BGY
                            {
                                if ((x.Substring(19, 3)) == (x2.Substring(19, 3)))//EA
                                {
                                    if ((x.Substring(27, 4)) == (x2.Substring(27, 4)))//SHSN
                                    {
                                        if ((x.Substring(31, 4)) == (x2.Substring(31, 4)))//HCN
                                        {

is the same as

if(x.Substring(12, 23)==x2.Substring(12,23))
{

Unless you are planning to add else statements to each section comparison you can just check the portion of the string in one go.

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.