File comparation

Carefull

Program.cs, Differ.cs

Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Xml.Serialization;

namespace p1exam
{
class Program
{
static string path1 = “f1.txt”;
static string path2 = “f2.txt”;
static int size = 0;
static int random = 0;
static int cat = 0;
static int rest = 0;
static List<Differ> colectie = new List<Differ>();
static Differ diferit = null;
static   FileStream f1 ;
static    FileStream f2;
static object obj = new object();

internal   class douaValori
{
public  int poz0;
public  int poz1;

}
public  static void comparare(object interv)
{
douaValori poz = (douaValori)interv;

int[] p=new int[2];
p[0]=poz.poz0;
p[1]=poz.poz1;

lock (obj)
{
Console.WriteLine(“Pozitii in apel:” +p[0] + ” to ” + p[1]);

f1.Seek((long)p[0], SeekOrigin.Begin);
f2.Seek((long)p[0], SeekOrigin.Begin);

byte[] buff1 = new byte[p[1] – p[0] + 1];
byte[] buff2 = new byte[p[1] – p[0] + 1];

f1.Read(buff1, 0, p[1] – p[0]);
f2.Read(buff2, 0, p[1] – p[0]);

for (int i = 0; i < buff1.Length; i++)
{
if (buff1[i] != buff2[i])
{
diferit = new Differ();
diferit.Pozitie = i;
diferit.Bit_diferit = buff1[i];

lock (colectie)
{
colectie.Add(diferit);
}
}
}

}

}

static void Main(string[] args)
{

f1 = new FileStream(path1, FileMode.Open, FileAccess.Read);
f2 = new FileStream(path2, FileMode.Open, FileAccess.Read);

if (f1.Length == f2.Length)
{
Console.WriteLine(“Sunt egale ca dimensiune!!!”);
size = (int)f1.Length;
Random r = new Random();
random = r.Next(0, size);
if (size % random != 0)
{
int[] poz = new int[2];
int i=0;
cat = (int)(size / random);
rest = size – (random * cat);
Thread[] threads = new Thread[cat + 1];
douaValori pozz = null;
poz[0] = 0;
for (i = 0; i < cat; i++)
{
if (i != 0)
poz[0] = poz[0] + random + 1;
poz[1] = poz[1] + random;
pozz = new douaValori();
pozz.poz0 = poz[0];
pozz.poz1 = poz[1];
Console.WriteLine(“Pozitii inainte de apel:” + poz[0] + ” to ” + poz[1]);
threads[i] = new Thread(new ParameterizedThreadStart(comparare));
threads[i].Start(pozz);
}

//restul
poz[0] = poz[1] + 1;
poz[1] = poz[1] + rest;
pozz = new douaValori();
pozz.poz0 = poz[0];
pozz.poz1 = poz[1];
threads[i] = new Thread(new ParameterizedThreadStart(comparare));
threads[i].Start(pozz);

for (int j = 0; j < (cat+1); j++)
threads[j].Join();

XmlSerializer x = new XmlSerializer(typeof(List<Differ>));
TextWriter w = new StreamWriter(“list.xml”);
x.Serialize(w, colectie);
w.Close();

}

}
else
Console.WriteLine(“Nu sunt egale ca dimensiune!!!”);
f1.Close();
f2.Close();
Console.ReadKey();
}
}
}

Differ.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace p1exam
{
public class Differ
{
private int pozitie;

public int Pozitie
{
get { return pozitie; }
set { pozitie = value; }
}
private byte bit_diferit;

public byte Bit_diferit
{
get { return bit_diferit; }
set { bit_diferit = value; }
}

public Differ()
{ }
}
}

Leave a Reply