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.

Questions by mani_2u4u

Showing Answers 1 - 15 of 15 Answers

nemuns

  • Jan 22nd, 2009
 

#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");
  }  
 }
}

chamkila

  • Jan 28th, 2009
 

/* 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

monuavi26

  • Mar 20th, 2009
 

/* 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;
}

iantuy

  • Apr 26th, 2009
 

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;
}

  Was this answer useful?  Yes

yongpark210

  • Jul 15th, 2010
 

#include <iostream>
using namespace std;
int CmpFiles(void);
int
_tmain(int argc, _TCHAR* argv[])
{
CmpFiles();
return 0;
}
int CmpFiles(void)
{
int cmp = 0;
FILE *pF1, *pF2;
char c1[16], c2[16];
pF1 = fopen("Doc1.txt", "r");pF2 = fopen(
"Doc2.txt", "r");
if ((pF1==NULL) || (pF2==NULL))cout << "Error in the filen";
else
{
while ((fgets(c1, 15, pF1)!=NULL) && (fgets(c2, 15, pF2))!=NULL)
{
puts(c1); // Sends output stdout
puts(c2); // Sends output stdout
// strcmp: A zero value indicates that both strings are equal.

_tmain( {CmpFiles(); 0; CmpFiles( { cmp = 0; c1[16], c2[16];, ((pF1==NULL) || (pF2==NULL))cout << { ((fgets(c1, 15, pF1)!=NULL) && (fgets(c2, 15, pF2))!=NULL)puts(c2);

cmp = strcmp(c1, c2);

//cmp = StringCompare(c1, c2);

if (cmp != 0)break;
}
}
fclose(pF1);
fclose(pF2);if (cmp == 0)
cout << "Files are same.n";
else
cout << "Files are different.n";
return cmp;
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions