GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 443 of 453    Print  
Compare Two Files in C
Write a program that compares two files and return 0 if they are equal and 1 if they are not equal.


  
Total Answers and Comments: 4 Last Update: April 26, 2009     Asked by: mani_2u4u 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: monuavi26
 
/* consider both the files have 15 characters*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmpfiles(void);
void main()
{
int result =cmpfiles();
printf("tn %d",result);
}

int cmpfiles(void)
{
FILE *fp1.*fp2;
fp1 = fopen(file1,'r'); // open the file1 to read its content
fp2 = fopen(file1,'r'); // open the file2 to read its content

char c1[15],c2[15]; //declaring a string to hold the content of each file

fgets(c1,16,fp1); //reading the content of file1 into c1
fgets(c2,16,fp2);//reading the content of file2 into c2

if(strcmp(c1,c2) = = 0)
 {
   return 1;
}

else return 0;
}

Above answer was rated as good by the following members:
mani_2u4u, manjunatha.dm86
January 22, 2009 16:22:36   #1  
nemuns Member Since: January 2009   Contribution: 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");
}
}
}

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
January 28, 2009 16:58:56   #2  
chamkila Member Since: January 2009   Contribution: 1    

RE: Compare Two Files in C

/* here is a very basic program to compare files - chamkila! */
#include
#include
#include

#define ANSI
#ifdef ANSI /* ANSI compatible version */
#include
void closeFiles(FILE* fh ...);
#else /* UNIX compatible version */
#include
void closeFiles(va_list);
#endif

int compareFiles(char[] char[]);
void printUsage(void);
void displayError(void);

int main (int argc char* argv[])
{
int rc errno;
if (argc < 3)
{
printUsage();
return -1;
}
rc compareFiles(argv[1] argv[2]);
printf( s: dn rc ? failed : success );
return 0;
}

int compareFiles(char arg1[] char arg2[])
{
FILE* fh1 NULL;
FILE* fh2 NULL;
char buf1[BUFSIZ] {''};
char buf2[BUFSIZ] {''};

if (strlen(arg1) < 0 || strlen(arg2) < 0)
{
printUsage();
return -1;
}
if (NULL (fh1 fopen(arg1 r )) || NULL (fh2 fopen(arg2 r )))
{
displayError();
return -1;
}

while (!feof(fh1) && !feof(fh2))
{
fgets(buf1 BUFSIZ - 1 fh1);
fgets(buf2 BUFSIZ - 1 fh2);
if (0 ! strcmp(buf1 buf2))
{
printf( file ' s' and file ' s' don't matchn arg1 arg2);
closeFiles(fh1 fh2);
return -1;
}
else
{
continue;
}
}
printf( file ' s' and file ' s' matchn arg1 arg2);
closeFiles(fh1 fh2);
return 0;
}

void printUsage(void)
{
printf( usage: fcmp.exe nn );
}

void displayError()
{
printf( error: sn strerror(errno));
return;
}

#ifdef ANSI /* ANSI compatible version */
void closeFiles(FILE* fh ...)
{
FILE* f NULL;
va_list argptr;

va_start(argptr fh);
f va_arg(argptr FILE*);
if (NULL ! f)
{
fclose(f);
}
va_end (argptr);

return;
}
#else /* UNIX compatible version must use old-style definition. */
void closeFiles(va_alist)
{
FILE* f NULL;
va_list argptr;

va_start(argptr);
f va_arg(argptr FILE*);
if (NULL ! f)
{
fclose(f);
}
va_end (argptr);
return;
}
#endif


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
March 20, 2009 08:33:10   #3  
monuavi26 Member Since: March 2009   Contribution: 2    

RE: Compare Two Files in C
/* consider both the files have 15 characters*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmpfiles(void);
void main()
{
int result cmpfiles();
printf("tn d" result);
}

int cmpfiles(void)
{
FILE *fp1.*fp2;
fp1 fopen(file1 'r'); // open the file1 to read its content
fp2 fopen(file1 'r'); // open the file2 to read its content

char c1[15] c2[15]; //declaring a string to hold the content of each file

fgets(c1 16 fp1); //reading the content of file1 into c1
fgets(c2 16 fp2);//reading the content of file2 into c2

if(strcmp(c1 c2) 0)
{
return 1;
}

else return 0;
}

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
April 26, 2009 14:48:20   #4  
iantuy Member Since: October 2008   Contribution: 2    

RE: Compare Two Files in C
int main(int argc char* argv[]) {
if(argc! 3) return -1;
FILE *fp1 *fp2;
fp1 fopen(argv[1] "r");
fp2 fopen(argv[2] "r");
if(!fp1 || !fp2) return -1;
while(fgetc(fp1) fgetc(fp2) && !feof(fp1));
return feof(fp1)&&feof(fp2)?0:1;
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape