GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Programming  >  C

 Print  |  
Question:  Compare Two Files in C

Answer: Write a program that compares two files and return 0 if they are equal and 1 if they are not equal.


January 01, 2009 16:22:36 #1
 nemuns   Member Since: January 2009    Total Comments: 3 

RE: Compare Two Files in C
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
 CmpFiles();
  return 0;
}

CmpFiles()
{
 FILE * fp1;
 FILE * fp2;
 char c1[100], c2[100];
 int cmp;
 
 fp1 = fopen("Doc1.txt", "r");
 fp2 = fopen("Doc2.txt", "r");
 
 if((fp1 == NULL) || (fp2 == NULL))
 {
  printf("Error in the file n");
 }
 else
 {
  while(fgets(c1 , 15, fp1) != NULL)     //reads from the file
   puts(c1);        //sends output to stdout
    
  while(fgets(c2 , 15, fp2) != NULL)
   puts(c2);
   
  if((cmp = strcmp(c1, c2)) == 0)
  {
   printf("Files are same n");
  }
  else
  {
   printf("Files are differnt n");
  }  
 }
}
     

 

Back To Question