Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two text files line by line [closed]

Beneath here i described a example Scenario:

"FileA-Database.txt" contains the following names:

KB200

KB300

KB400

"FileB-Slave.txt" contains the following names:

KB600

KB200

KB400

KB700

I want to compare the "FileA-Database.txt" with "FileB-Slave.txt" and let the missing values be filled in automatically in the "FileA-Database.txt" file also i need to display the missing values in a text file called "Results.txt".

The code needs to be compatible with C# (framework 4.0+) please!.

I need a simple approach, mine doesnt work exactly the way i want it to:

    private void button_compare_Click(object sender, EventArgs e)
        {
            string fileA, fileB, fileC;
            fileA = "database-critical.txt";
            fileB = "patchlist.txt";
            fileC = "result.txt";

            string alphaFilePath = fileA;

            List<string> alphaFileContent = new List<string>();

            using (FileStream fs = new FileStream(alphaFilePath, FileMode.Open))
            using(StreamReader rdr = new StreamReader(fs))
            {
                while(!rdr.EndOfStream)
                {
                    alphaFileContent.Add(rdr.ReadLine());
                }
            }

            string betaFilePath = fileB;

            StringBuilder sb = new StringBuilder();


            using (FileStream fs = new FileStream(betaFilePath, FileMode.Open))
            using (StreamReader rdr = new StreamReader(fs))
            {
                while(! rdr.EndOfStream)
                {
                    string[] betaFileLine = rdr.ReadLine().Split(Convert.ToChar(","));

                    if (alphaFileContent.Contains(betaFileLine[0]))
                    {
                        sb.AppendLine(String.Format("{0}", betaFileLine[0]));
                    }
                }
            }

using (FileStream fs = new FileStream(fileC, FileMode.Create))
        using (StreamWriter writer = new StreamWriter(fs))
        {
            writer.Write(sb.ToString());
        }
    }

            //End
        }
like image 844
Developman Avatar asked Jan 16 '13 23:01

Developman


People also ask

How do I compare data in two text files?

Using File Compare or the FC command in Command Prompt is another way if you need text or binary compare. The output is shown in Command Prompt and is not easy to read. For all file formats that Word can open, the Compare option in Word is the easiest to use.

Which command is used to compare files line by line?

diff stands for difference. This command is used to display the differences in the files by comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us which lines in one file have is to be changed to make the two files identical.

How can I tell if two files have the same contents?

Comparison Using cmp GNU cmp compares two files byte by byte and prints the location of the first difference. We can pass the -s flag to find out if the files have the same content. Since the contents of file1 and file2 are different, cmp exited with status 1.


2 Answers

String directory = @"C:\Whatever\";
String[] linesA = File.ReadAllLines(Path.Combine(directory, "FileA-Database.txt"));
String[] linesB = File.ReadAllLines(Path.Combine(directory, "FileB-Database.txt"));

IEnumerable<String> onlyB = linesB.Except(linesA);

File.WriteAllLines(Path.Combine(directory, "Result.txt"), onlyB);
like image 189
Tommaso Belluzzo Avatar answered Oct 18 '22 03:10

Tommaso Belluzzo


Since your question seems like you made no effort whatsoever I'll give you just a rough outline.

  1. Read both files line by line, e.g. with File.ReadAllLines or File.ReadLines.
  2. Use LINQ's Except method.
  3. Write the results into a new file.
like image 39
Joey Avatar answered Oct 18 '22 04:10

Joey