PURPOSE :: see statement in comments
GOT: Segmentation Fault
I guess the segfault is sourced in the compile-time warning but I am
giving a char* to the function already.
/* K&R2, section 7.7, exercise 7.6
*
* write a program to compare 2 files, printing that first line
* where they differ.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum MAXSIZE { ARRSIZE=1000 };
void compare_files( FILE*, FILE* );
void print_line( char* );
int main( int argc, char* argv[] )
{
FILE *pf1, *pf2;
if( argc !=3 )
{
fprintf( stderr, "You iDiOT, I expect 2 files as input. \n" );
exit(EXIT_FAILU RE);
}
/* open files */
pf1 = fopen( *++argv, "r" );
pf2 = fopen( *++argv, "r" );
/* error check */
if( pf1 == NULL || pf2 == NULL )
{
fprintf( stderr, "error opening files\n");
exit(EXIT_FAILU RE);
}
else
{
compare_files( pf1,pf2 );
}
/* don't forget to close the files */
fclose( pf1 );
fclose( pf2 );
return 0;
}
/* compare 2 files */
void compare_files( FILE* pf1, FILE* pf2 )
{
int c1, c2, match;
char *line1, *line2;
char *begin_line1, *begin_line2;
match = 1;
while( ((line1 = fgets( line1, ARRSIZE, pf1 )) != NULL) ||
((line2 = fgets( line2, ARRSIZE, pf2 )) != NULL))
{
begin_line1 = line1;
begin_line2 = line2;
for( c1 = *line1, c2 = *line2; c1 != '\0' && c2 != '\0'; ++line1,
++line2 )
{
if ( c1 != c2 )
{
match = 0;
print_line( begin_line1 );
printf("\n-----------------------\n"); print_line( begin_line2 );
}
}
}
}
void print_line( char* line )
{
printf("%s\n", *line++);
}
=============== === OUTPUT =============== =======
[arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra 7-6.c
7-6.c: In function `print_line':
7-6.c:88: warning: format argument is not a pointer (arg 2)
[arnuld@raj C]$ ./a.out
You iDiOT, I expect 2 files as input.
[arnuld@raj C]$ ./a.out 7-6.c 5-4.c
Segmentation fault
--
my email ID is at the above address
GOT: Segmentation Fault
I guess the segfault is sourced in the compile-time warning but I am
giving a char* to the function already.
/* K&R2, section 7.7, exercise 7.6
*
* write a program to compare 2 files, printing that first line
* where they differ.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum MAXSIZE { ARRSIZE=1000 };
void compare_files( FILE*, FILE* );
void print_line( char* );
int main( int argc, char* argv[] )
{
FILE *pf1, *pf2;
if( argc !=3 )
{
fprintf( stderr, "You iDiOT, I expect 2 files as input. \n" );
exit(EXIT_FAILU RE);
}
/* open files */
pf1 = fopen( *++argv, "r" );
pf2 = fopen( *++argv, "r" );
/* error check */
if( pf1 == NULL || pf2 == NULL )
{
fprintf( stderr, "error opening files\n");
exit(EXIT_FAILU RE);
}
else
{
compare_files( pf1,pf2 );
}
/* don't forget to close the files */
fclose( pf1 );
fclose( pf2 );
return 0;
}
/* compare 2 files */
void compare_files( FILE* pf1, FILE* pf2 )
{
int c1, c2, match;
char *line1, *line2;
char *begin_line1, *begin_line2;
match = 1;
while( ((line1 = fgets( line1, ARRSIZE, pf1 )) != NULL) ||
((line2 = fgets( line2, ARRSIZE, pf2 )) != NULL))
{
begin_line1 = line1;
begin_line2 = line2;
for( c1 = *line1, c2 = *line2; c1 != '\0' && c2 != '\0'; ++line1,
++line2 )
{
if ( c1 != c2 )
{
match = 0;
print_line( begin_line1 );
printf("\n-----------------------\n"); print_line( begin_line2 );
}
}
}
}
void print_line( char* line )
{
printf("%s\n", *line++);
}
=============== === OUTPUT =============== =======
[arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra 7-6.c
7-6.c: In function `print_line':
7-6.c:88: warning: format argument is not a pointer (arg 2)
[arnuld@raj C]$ ./a.out
You iDiOT, I expect 2 files as input.
[arnuld@raj C]$ ./a.out 7-6.c 5-4.c
Segmentation fault
--
my email ID is at the above address
Comment