K& R2,练习7.6
目的::参见评论中的陈述
GOT:分段错误
我猜这段错误来自编译时警告但我已经
已经为这个功能提供了一个char *。
/ * K& R2,7.7节,练习7.6 >
*
*写一个程序来比较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 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_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.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
--
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.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
你iDiOT,我希望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
分段错误
-
http://lispmachine.wordpress.com/
我的电子邮件地址在上述地址
./a.out 7-6.c 5-4.c
Segmentation fault
--
http://lispmachine.wordpress.com/
my email ID is at the above address