Click here to Skip to main content
15,560,045 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have to create 2 text files in c#.and there is some lines in two files. some lines are same. some lines are completely differnt. some lines are partially similar. and then read the data from those files. and need 4 buttons for comparison.each button has a question and click on that button will give the ans in a label or text box.
the question for comaparison are below.

1. how many lines are similar.
2. how many lines are partial similar(match some character). many lines are dissimilar according to wordcoundwise.
4. how many lines are dissimilar according to wordwise.

i m new in file handling. please help me for making this. this is my interview question so please fully help needed.

So, you have a GUI app with some buttons and some result display area.
Plus you have some business logic that does the loading and comparison of the two files.

Let's now focus on the business logic.
It consists of
- LoadFirstFile(...)
- LoadSecondFile(...)
- GetNumberOfEqualLines(...)
- GetNumberOfPartiallyEqualLines(...)
- etc.

If you find a library that provides the comparison functionality, use it in the above mentioned functions.

If you have no such library, implement it yourself.
The trouble with comparing is the algorithm to synchronize after a mismatch.

Assume you compare from begin to end of the two files: at one point you reach a line in both files where you have a mismatch. Does the line is *missing* in the 1st/2nd file or are the lines not missing but *partially* equal?

E.g. comparing the two files



You have to decide if a line is missing or if it is a partial match.

Think of how to achieve that. Write the cases down on paper and try to simulate the situations.

Depending on the size of the data and the needed "accuracy", you might come up with some context range where you search for a synchronization point. E.g. if you say you search at a mismatch for the next N lines for a matching in both files. The nearest match is taken and the lines between are different (or partially equal).

At this point you get into an interesting interview discussion... ;-)

Good luck!

Share this answer

In my Tip/Trick i have created one API for comparison of two text messages for similarity. you can find it Here[^]

From above example you can get some idea about how you can achieve your goal. you need to create two text reader for two file comparison. After each of the read method call you can compare both the line and increment the counter depending on the button click event.

if you would like to create this utility then you need knowledge in File Handling. Please check this article[^] to understand file handling in c#.

Hope above information will help you to develop your program.

-Amit Gajjar
Share this answer
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