Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a multiple text files and they have content!I need to get all contents and compare them,So I tried reading and storing them in list as each text file context as index of list
like
1.txt in [0] and next 2.txt in [1] and so on...
but i cant compare those... Please Can any one help me!

example :1.txt has values 1 2 3 4
2.txt has values 3 5 6
I should get output as 3 i.e common context in both files similarly if I have many files common content of those all

What I have tried:

I tried using list where all text files are stored in index but i can't compare those!I am beginner and learning c#
Posted
Updated 29-Jan-17 6:14am
v3
Comments
[no name] 29-Jan-17 8:46am    
I would think that a Dictionary would be more suited to the task.
Member 12972565 29-Jan-17 8:51am    
I will be glad if you can help little bit with code!thank you
[no name] 29-Jan-17 9:33am    
Yes I am sure that you would. Learning to write code is part of the learning process. You should try it.
Richard MacCutchan 29-Jan-17 9:46am    
Start with just 2 files and try comparing them. It is not clear what you are trying to compare, is it just common words or full sentences?
Member 12972565 29-Jan-17 10:03am    
just a numbers in a single row

1 solution

This isn't complex if you think about it and take is stage by stage.
Read each file into a string:
C#
string data = File.ReadAllText(pathToFile);

Break that into individual segments to compare:
C#
string[] bits = data.Split(' ', '\n');

If you add each of these to a collection, you can later use a loop to compare them:
C#
List<string[]> toCompare = new List<string[]>();
...
toCompare.Add(bits);

And the loop is pretty simple:
C#
string[] soFar = toCompare[0];
for (int i = 1; i < toCompare.Count; i++)
    {
    soFar = soFar.Intersect(toCompare[i]).ToArray();
    if (soFar.Count() == 0) break;
    }
The Linq Intersect method returns all the common values in the two collections.
 
Share this answer
 

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