c - How do I access a function variable from main() -
i'm attempting use character compare function in main() while ignoring case sensitivity. there way call toupper(ch1) , toupper(ch2) main if -i (for case insensitivity) raised can reuse code.
int charactercompare( file *file1, file *file2, char file1name[], char file2name[] ) { int ch1, ch2; int differ = 0; { ch1 = fgetc( file1 ); ch2 = fgetc( file2 ); differ++; if ( feof( file1 ) && !feof( file2 ) ) { printf( "eof on %s\n", file1name ); return 1; } else if ( feof( file2 ) && !feof( file1) ) { printf( "eof on %s\n", file2name ); return 1; } if (ch1 != ch2) { printf( "files differ: char %d\n", differ ); return 1; } } while( ( !feof( file1 ) ) && ( !feof( file2 ) ) && (ch1 == ch2) ); printf( "files equal\n" ); return 0; }
long answer (not tested, shoukld idea):
int compare(char ch1, char ch2, int ignorecase) { if (ignorecase) { ch1 = toupper(ch1) ; ch2 = toupper(ch2) ; } return ch1 == ch2 ; } int charactercompare( file *file1, file *file2, char file1name[], char file2name[], int ignorecase ) { int ch1, ch2; int differ = 0; { ch1 = fgetc( file1 ); ch2 = fgetc( file2 ); differ++; if ( feof( file1 ) && !feof( file2 ) ) { printf( "eof on %s\n", file1name ); return 1; } else if ( feof( file2 ) && !feof( file1) ) { printf( "eof on %s\n", file2name ); return 1; } if (compare(ch1, ch2, ignorecase)) { printf( "files differ: char %d\n", differ ); return 1; } } while( ( !feof( file1 ) ) && ( !feof( file2 ) ) && (ch1 == ch2) ); printf( "files equal\n" ); return 0; }
and in main e.g :
ignorecase = argv[1] == "-i" ; int different = charactercompare(f2, f2, name1, name2, ignorecase) ;
Comments
Post a Comment