* *写一个程序来比较2个文件,打印第一行 *它们不同的地方。 * * / #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,"你iDiOT,我希望2个文件作为输入。\ n"); 退出(EXIT_FAILURE); } / *打开文件* / pf1 = fo笔(* ++ argv," r" ); pf2 = fopen(* ++ argv," r"); / *错误检查* / if(pf1 == NULL || pf2 == NULL) { fprintf(stderr,错误打开文件\ n); 退出(EXIT_FAILURE); } 其他 { compare_files(pf1,pf2) ; } / *别忘了关闭文件* / fclose(pf1); fclose(pf2); 返回0; } / *比较2个文件* / 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!=''\'''; ++ line1, ++ line2) { if(c1!= c2) { match = 0; print_line(begin_line1); printf(" \\\ ----------- ------------ \\\"); print_line(begin_line2); } } } } void print_line(char * line) { printf("%s \ n",* line ++); } ==================输出================== ==== [arnuld @ raj C] $ gcc -ansi -pedantic -Wall -Wextra 7-6.c 7-6.c:in function` print_line'': 7-6.c:88:警告:格式参数不是指针(arg 2) [arnuld @ raj C] $ ./a.out 你iDiOT,我希望2个文件作为输入。 [arnuld @ raj C] $ ./a.out 7- 6.c 5-4.c 分段错误 - http://lispmachine.wordpress.com/ 我的电子邮件ID在上面的地址PURPOSE :: see statement in commentsGOT: Segmentation FaultI guess the segfault is sourced in the compile-time warning but I amgiving 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_FAILURE);}/* 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_FAILURE);}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.c7-6.c: In function `print_line'':7-6.c:88: warning: format argument is not a pointer (arg 2)[arnuld@raj C]$ ./a.outYou iDiOT, I expect 2 files as input.[arnuld@raj C]$ ./a.out 7-6.c 5-4.cSegmentation fault-- http://lispmachine.wordpress.com/my email ID is at the above address推荐答案 gcc -ansi -pedantic -Wall -Wextra 7-6.c 7-6.c:在函数`print_line'': 7-6.c:88:警告:表格在参数不是指针(arg 2) [arnuld @ raj C]gcc -ansi -pedantic -Wall -Wextra 7-6.c7-6.c: In function `print_line'':7-6.c:88: warning: format argument is not a pointer (arg 2)[arnuld@raj C] ./ a.out 你iDiOT,我希望2个文件作为输入。 [arnuld @ raj C]./a.outYou iDiOT, I expect 2 files as input.[arnuld@raj C] ./ a.out 7-6。 c 5-4.c 分段错误 - http://lispmachine.wordpress.com/ 我的电子邮件地址在上述地址./a.out 7-6.c 5-4.cSegmentation fault-- http://lispmachine.wordpress.com/my email ID is at the above address 这篇关于K&amp; R2,练习7.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 16:55