Write a program to compare two text files. The names of the files are to be supplied as command line arguments. The program should write a message to the screen indicating that the files are exactly the same or that there are differences. If the files differ, the program should write to the screen the line numbers for the lines that are not the same.

i have this one almost right, but my program will only find the first difference and i have to find all the differences... how do you suggest i go about this?

#include <stdio.h>

int main(int argc, char *argv[]){
  char ch1, ch2, same;
  int l;
  FILE *fp1, *fp2;

  fp1 = fopen( argv[1], "r" );
  fp2 = fopen( argv[2], "r" );

  l = 1;
  same = 1;

  while( ch1 != EOF ){
    ch1 = fgetc( fp1 );
    ch2 = fgetc( fp2 );

    if( ch1 != ch2 ){
      printf( "\nFiles differ at line number %d\n", l );
      same = 0;
      break;
    }

    if( ch1 == '\n' ) l++;
  }

  if( same ) printf("\nFiles are the same.\n");

  fclose( fp1 );
  fclose( fp2 );

  return(0);
}

Recommended Answers

All 6 Replies

>> while( ch1 != EOF ){

That won't work because (1) ch1 must be an integer, not char and (2) EOF is not known until it attempts to read beyond end-of-file, not before.

Don't read the file one character at a time, but one line at a time. Use fgets() to read each line, then compare them using strcmp().

Line 19 does not satisfy the requirements of the program because you also have to print out all the line numbers that do not match. That means you have to keep track of line numbers -- fgets() will help make that very easy because all you have to do is increment a counter each time fgets() is called. If the two lines are not the same then just display the line number and continue on reading the files.

I could post some code, but then I'd ruin the fun you will have of completing the assignment :)

thanks though, posting code is an easy grade but making me do it helps me learn it.

its taken me a while to get to this program
but it works very well now, i was just wondering why it won't state the files are different if one file contains more lines then the other

heres my code

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]){
  char s1[50], s2[50];
  int line, same;
  FILE *fp1, *fp2;

  fp1 = fopen( argv[1], "r" );
  fp2 = fopen( argv[2], "r" );

  if( fp1 == NULL ){
    printf( "Error: can't read first file.\n" );
    return 1;
  }
  if( fp2 == NULL ){
    printf( "Error: can't read second file.\n" );
    return 1;
  }

  line = 1;

  while( fgets( s1, 50, fp1 ) != NULL && fgets( s2, 50, fp2 ) != NULL ){

    if( strcmp( s1, s2 ) != 0 ){
      printf( "\nFiles differ at line number %d\n", line );
      same = 0;
    }

    if( s1 == NULL && s2 != NULL ){
      printf( "\nFiles differ at line number %d\n", line );
      same = 0;
    }
    }

    line++;
  }
  if( same ) printf("\nFiles are the same.\n");

  fclose( fp1 );
  fclose( fp2 );

  return(0);
}

'same' is never initialized. if a non-matching line is never found to set it to 0, what value will it contain? The answer is: who the hell knows?

so when one file read in the while() loop returns a NULL, the && condition will evaluate false. at which point the loop will exit, and if no non-matching lines were previously found, the variable 'same' will likely contain negative eleventyseventhousand, and the program will tell you the files are the same.

also youve got one too many closing brackets around the end of the while loop, probably a cut and paste error cuz i doubt that would compile.

all in all, not too bad... it could be a lot worse :P


.

i edited the code a little bit but it still says my two files are the same when one has 2 extra lines of words.

edited code:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]){
  char s1[50], s2[50];
  int line = 1, same = 1;
  FILE *fp1, *fp2;

  fp1 = fopen( argv[1], "r" );

  if( fp1 == NULL ){
    printf( "Error: can't read first file.\n" );
    return 1;
  }

  fp2 = fopen( argv[2], "r" );
  if( fp2 == NULL ){
    printf( "Error: can't read second file.\n" );
    return 1;
  }

  while( fgets( s1, 50, fp1 ) != NULL && fgets( s2, 50, fp2 ) != NULL ){

    if( strcmp( s1, s2 ) != 0 ){
      printf( "\nFiles differ at line number %d\n", line );
      same = 0;
    }

    if( s1 == NULL && s2 != NULL ){
      printf( "\nFiles differ at line number %d\n", line );
      same = 0;
    }
    if( s1 != NULL && s2 == NULL ){
      printf( "\nFiles differ at line number %d\n", line );
      same = 0;
    }

    line++;
  }
  if( same ) printf("\nFiles are the same.\n");

  fclose( fp1 );
  fclose( fp2 );

  return(0);
}

After line 39 you need to add something to check if either of the two files have not reached EOF. My suggestion is to add two pointers, one for each file

char *p1, *p2;
...
  p1 = p2 = NULL;
  while( (p1 = fgets( s1, sizeof(s1), fp1 )) != NULL 
      && (p2 = fgets( s2, sizeof(s2), fp2 )) != NULL ){

    if( strcmp( s1, s2 ) != 0 ){
      printf( "\nFiles differ at line number %d\n", line );
      same = 0;
    }

    if( s1 == NULL && s2 != NULL ){
      printf( "\nFiles differ at line number %d\n", line );
      same = 0;
    }
    if( s1 != NULL && s2 == NULL ){
      printf( "\nFiles differ at line number %d\n", line );
      same = 0;
    }

    line++;
  }
  if( p1 == NULL && p2 != NULL)
  {
      printf("\nFile 2 is larger than file 2\n");
  }
  else if( p2 == NULL && p1 != NULL )
  {
      printf("\nFile 1 is larger than file 2\n");
  }
  if( same ) printf("\nFiles are the same.\n");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.