ccplusplus.com
Learn C, C++ Concepts
Saturday, January 7, 2012
compare two files in c
/******************************************************************* * File : compare-two-files.c * Author : Saurabh Gupta * Description : compare two files in c * Date : PM 11:04 07 January 2012 * Source : http://www.ccplusplus.com/p/c.html *******************************************************************/ #include <stdio.h> void main() { FILE *fp1, *fp2, *fopen(); int ca, cb; char fname1[40], fname2[40] ; printf("Enter first filename:") ; gets(fname1); printf("Enter second filename:"); gets(fname2); fp1 = fopen( fname1, "r" ); /* open for reading */ fp2 = fopen( fname2, "r" ) ; /* open for writing */ if ( fp1 == NULL ) /* check does file exist etc */ { printf("Cannot open %s for reading \n", fname1 ); exit(1); /* terminate program */ } else if ( fp2 == NULL ) { printf("Cannot open %s for reading \n", fname2 ); exit(1); /* terminate program */ } else /* both files opened successfully */ { ca = getc( fp1 ) ; cb = getc( fp2 ) ; while ( ca != EOF && cb != EOF && ca == cb ) { ca = getc( fp1 ) ; cb = getc( fp2 ) ; } if ( ca == cb ) printf("Files are identical \n"); else if ( ca != cb ) printf("Files differ \n" ); fclose ( fp1 ); fclose ( fp2 ); } } /* * OUTPUT * [sgupta@rhel6x64 file-handling]$ gcc compare-two-files.c -o compare-two-files [sgupta@rhel6x64 file-handling]$ ./compare-two-files Enter first filename:compare-two-files.c Enter second filename:compare-two-files.c Files are identical [sgupta@rhel6x64 file-handling]$ ./compare-two-files Enter first filename:count-characters-file.c Enter second filename:compare-two-files.c Files differ [sgupta@rhel6x64 file-handling]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment