#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 100
typedef char string;
void FindSimillar(string FirstFile, string SecondFile, int vec);
void FindDifference(string FirstFile, string SecondFile, int vec);
void Add(string NewFile, string FirstFile, string SecondFile);
int ShowMenu (void);
void main(void)
{
string str1;
string str2;
string Main;
int vec;
int index = 0;
int menu = 1;
while (menu)
{
system("cls");
menu = ShowMenu();
switch (menu)
{
case 1:
printf("enter the first path: ");
scanf("%s",str1);
printf("enter the second path: ");
scanf("s",str2);
FindSimillar(str1, str2,vec);//activates thr function
while (vec!=-1)//until the end of the vec
{
printf("line %d.",vec);//prints
}
printf ("\nDone.");
break;
case 2:
printf("enter the first path: ");
gets(str1);
printf("enter the second path: ");
gets(str2);
FindDifference(str1, str2, vec);//activates the functuin
while (vec!=-1)//until the end of the vec
{
printf("line %d.",vec);//prints
}
printf ("\nDone.");
break;
case 3:
printf("enter the new file's path: ");
gets(Main);
printf("enter the first file's path: ");
gets(str1);
printf("enter the second file's path: ");
gets(str2);
Add(Main, str1, str2);//activates the function
printf ("\nDone.");
break;
case 4:
printf ("\nexiting...");
break;
default:
printf ("\nEnter 1-4");
}
getch();
}
}
//---------------------------------------------------------------------------------------------------------------------
void FindSimillar(string FirstFile, string SecondFile, int vec)
{
FILE *cmpr1;
FILE *cmpr2;
int line=0;
int index=0;
string ff;//first string
string sf;//second string
cmpr1 = fopen (FirstFile,"rt");
cmpr2 = fopen (SecondFile,"rt");
while ((!feof(cmpr1)) && (!feof(cmpr2)))//while its not the end of any of the files
{
fgets (ff, LEN, cmpr1);//get the line/string
fgets (sf, LEN, cmpr2);//get the line/string
line++;
if (strcmp (ff, sf)==0)//if they're equal
{
vec=line;//puts in the vector the line's number
index++;
}
}
vec=-1;//a sign to recoginize the end of file
fclose (cmpr1);
fclose (cmpr2);
}
//---------------------------------------------------------------------------------------------------------------------
void FindDifference(string FirstFile, string SecondFile, int vec)
{
FILE *cmpr1;
FILE *cmpr2;
int line=0;
int index=0;
string ff;//first string
string sf;//second string
cmpr1 = fopen (FirstFile,"rt");
cmpr2 = fopen (SecondFile,"rt");
while ((!feof(cmpr1)) && (!feof(cmpr2)))
{
fgets (ff, LEN, cmpr1);//gets the line/string
fgets (sf, LEN, cmpr2);//gets the line/string
line++;
if (strcmp (ff, sf))//if they're different
{
vec=line;//puts the line's number
index++;
}
}
vec=-1;
fclose (cmpr1);
fclose (cmpr2);
}
//----------------------------------------------------------------------------------------------------------------------
void Add(string NewFile, string FirstFile, string SecondFile)
{
FILE *New; // a pointer to the source file.
FILE *First;
FILE *Sec; // a pointer to the target file.
string file; // a row to save lines from the source file.
New = fopen (NewFile, "at");
First = fopen (FirstFile,"rt");
Sec = fopen (SecondFile,"rt");
if ((!New) || (!First) || (!Sec))
{
exit (1);
}
// process
while (fgets (file, 100, First))
{
fputs (file, New);
}
while (fgets (file, 100, Sec))
{
fputs (file, New);
}
// closing files
fclose (New);
fclose (First);
fclose (Sec);
}
//----------------------------------------------------------------------------------------------------------------------
int ShowMenu (void)
{
// variable definition
int menu;
// code section
// menu printing
printf ("***Menu***\n");
printf ("1. compare one file to another and finds the lines which are simillar\n");
printf ("2. compare one file to another and finds the lines which are different\n");
printf ("3. Adding to a new file - two others files\n");
printf ("0. exit\n");
// choise input
printf ("Please enter your choise now 1-3 (Exit - 0): ");
scanf ("%d", &menu);
printf ("\n");
return menu;
}