Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,

I'm trying to compare lines in a text file , but the examples I've seen are a bit complicated someone could give me a simple example of how to do it .

example :

"123" , " 234 "
"123" , " 234 " Compare this line to the top
" 134 " , " 664 "

and in the end only show this:

"123" , " 234 "
" 134 " , " 664 "

Thanks

Code:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Collections;

namespace ReadTxt
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader read = new StreamReader(@"C:\File.txt");
            String line = read.ReadToEnd();
            
            //Do stuff

                Console.WriteLine(line);
                Console.ReadLine();
            
            }           
        }
 }
Posted
Updated 11-Jan-15 10:26am
v3
Comments
Zoltán Zörgő 11-Jan-15 16:20pm    
Does not look complicated at all. What have you tried so far?
Sergey Alexandrovich Kryukov 11-Jan-15 16:25pm    
What is complicated? :-) Better please tell us what are you missing. Reading a file? Reading a line? String comparison?
—SA
Laxmax1 11-Jan-15 16:27pm    
I'm a novice programmer , and the examples that I find not see very clear.
BillWoodruff 11-Jan-15 17:00pm    
Is your goal here to only eliminate duplicates when an identical line follows the previous line ... in other words, you don't care if this occurs:

"123" , " 234 "
" 134 " , " 664 "
"123" , " 234 "

So, in that case you would write out the identical line twice ?

Well. Supposing that you need to compare lines using char by char equality, it can be as simple as this:
C#
var result = File.ReadLines(@"D:\TEMP\x.txt").Distinct();

Still, if you need performance, you need to dig deeper to optimize. I have to admit, this is not optimized for comparing only the two consecutive rows. It is doing overall search.
To take only previous line into account, you can use this one instead:
C#
string prev = null; 
foreach(var curr in File.ReadLines(@"D:\TEMP\x.txt"))
{
	if(curr != prev) Console.WriteLine(curr);
	prev = curr;
}
 
Share this answer
 
v3
Comments
Laxmax1 11-Jan-15 16:35pm    
Thanks :)
Zoltán Zörgő 11-Jan-15 16:39pm    
You are welcome. See update also.
Laxmax1 12-Jan-15 14:22pm    
My final solucion is this:

string lastLine = " ";
while (reader.Peek() >= 0 )
{
var lines = reader.ReadLine();

if (lines != lastLine){

writer.WriteLine(lines);
lastLine = lines;
}
Zoltán Zörgő 12-Jan-15 14:23pm    
Of course, this will also work. :)
Laxmax1 12-Jan-15 14:25pm    
thanks for help me bro! :)
It does not require "ideas". Please see my comment to the answer.
Reading file? Please see http://msdn.microsoft.com/en-us/library/system.io.file.readalllines%28v=vs.110%29.aspx[^] or http://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx[^].
Line comparison? '=='.
Figuring out what to compare with what? Use this: http://en.wikipedia.org/wiki/Brain[^].

[EDIT]

Now I can see your updated question. First, better use using statement: http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^],
http://msdn.microsoft.com/en-us/library/aa664736%28v=vs.71%29.aspx[^].

More importantly, use my last link before [EDIT]. Why reading to the end? The text read to the end is not one text line. You need to read line one by one.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900