Skip to content

Instantly share code, notes, and snippets.

@halit
Created April 25, 2013 12:43
Show Gist options
  • Select an option

  • Save halit/5459424 to your computer and use it in GitHub Desktop.

Select an option

Save halit/5459424 to your computer and use it in GitHub Desktop.
Two file comparer
/*
* =====================================================================================
*
* Filename: tfc.c
*
* Description: Two File Comparer
*
* Version: 1.0
* Created: 25-04-2013 15:00:15
* Compiler: gcc
*
* Author: Halit Alptekin, info@halitalptekin.com
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int main(int argc, const char *argv[])
{
void compare(FILE*, FILE*);
FILE *fp1, *fp2;
if (argc == 3){
if ((fp1 = fopen(*++argv, "r")) == NULL){
fprintf(stderr, "%s: dosya acilamadi\n", *argv);
exit(2);
} else if ((fp2 = fopen(*++argv, "r")) == NULL){
fprintf(stderr, "%s: dosya acilamadi\n", *argv);
exit(2);
} else {
compare(fp1, fp2);
fclose(fp1);
fclose(fp2);
exit(0);
}
} else {
fprintf(stdout, "kullanim ./tfc file1 file2 \n");
exit(1);
}
return 0;
}
void compare(FILE *fp1, FILE *fp2){
char line1[MAX], line2[MAX];
char *lp1, *lp2;
do{
lp1 = fgets(line1, MAX, fp1);
lp2 = fgets(line2, MAX, fp2);
if(lp1 == line1 && lp2 == line2){
if(strcmp(line1, line2) != 0){
fprintf(stdout, "ilk farkli satir\n%s\n", line1);
lp1 = lp2 = NULL;
}
}else if(lp1 != line1 && lp2 == line2){
fprintf(stdout, "ilk dosyanin son satiri\n%s\n", line2);
}else if(lp1 == line1 && lp2 != line2){
fprintf(stdout, "ikinci dosyanin son satiri\n%s\n", line1);
}
}while(lp1 == line1 && lp2 == line2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment