Сравнение двух текстовых файлов - C# (179854)
Формулировка задачи:
Добрый вечер! Задачка такая стоит: Сравнить два текстовых файла. Определить одинаковые ли они? То есть соответствуют ли соответствующие символы у строчках и способ разбиения на строчки). Вот мой код:
ну по сути вот сам алгоритм
Содержимое файлов от балды,к примеру
file kick detroit nuttertools
regex un pol het hate tools extra metallica
slipknot Rammstein
Вроде сравнивает всё,но правильно я сделал? Если я к примеру ставлю пробел после последнего слова и сохраняю,то уже разные. А можно к примеру сделать без пробелов и абзацов(чтоб они не считались,а считались именно символы(с точки зрения юзера,к примеру))? Подскажите пожалуйста как. msdn читал и FAQ тоже,все вроде понимаю,но всё таки..
using System;
using System.IO;
namespace laba10_part2
{
class Program
{
static void Logic(string path, string path1) {
StreamReader first = new StreamReader(path);
StreamReader second = new StreamReader(path1);
int i = 0;
int m = 0;
string count1=null;
string count2 = null;
while (!first.EndOfStream) {
i++;
count1 += first.ReadLine();
}
while (!second.EndOfStream)
{
m++;
count2 += second.ReadLine();
}
if (count1 != count2)
{
Console.WriteLine("Разные");
}
else {
Console.WriteLine("Одинаковые");
}
}
static void Read(string path,string path1) {
StreamReader first = new StreamReader(path);
Console.WriteLine("Первый файл\n");
while (!first.EndOfStream) {
Console.WriteLine(first.ReadToEnd());
}
Console.WriteLine("Второй файл\n");
StreamReader second = new StreamReader(path1);
while (!second.EndOfStream)
{
Console.WriteLine(second.ReadToEnd());
}
} //Выводим содержимое файлов
static void Main(string[] args)
{
try
{
Console.WriteLine("***");
string path = @"D:\proga\file1.txt";
string path1 = @"D:\proga\file2.txt";
Read(path, path1);
Logic(path,path1);
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
}
}StreamReader first = new StreamReader(path);
StreamReader second = new StreamReader(path1);
int i = 0;
int m = 0;
string count1=null;
string count2 = null;
while (!first.EndOfStream) {
i++;
count1 += first.ReadLine();
}
while (!second.EndOfStream)
{
m++;
count2 += second.ReadLine();
}
if (count1 != count2)
{
Console.WriteLine("Разные");
}
else {
Console.WriteLine("Одинаковые");
}Решение задачи: «Сравнение двух текстовых файлов»
textual
Листинг программы
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
string file1 = GetMd5(File.ReadAllBytes(@"D:\file1.txt"));
string file2 = GetMd5(File.ReadAllBytes(@"D:\file2.txt"));
if (file1 == file2)
{ Console.WriteLine("Файлы одинаковые"); }
else { Console.WriteLine("Файлы НЕ одинаковые"); }
Console.ReadKey();
}
public static string GetMd5(byte[] b)
{
MD5 md5Hash = MD5.Create();
byte[] data = md5Hash.ComputeHash(b);
StringBuilder hash = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
hash.Append(data[i].ToString("x2"));
}
return hash.ToString();
}
}
}