a.txt b.txt
1231435 2342424
3425255 6758582
3226342 2354255<-
2542525 7895422
-> 2354255 9865433
结果应是ch[ 5 ].Flag =1,其他为0.可结果不对!请大家赐教!
#include<stdio.h>
main()
{
FILE *fp;
FILE *fpp;
int record;
int conum;
struct ch { char txt[ 7 ]; int Flag; }ch[ 10 ];
struct ci { char txt[ 7 ]; }ci[ 10 ];
fp = fopen( "a.txt" , "rb" );
if( fp == NULL )
{
printf( "\na.txt is NULL!!!");
return( 0 );
}
fpp = fopen( "b.txt" , "rb" );
if( fpp == NULL )
{
printf( "\nb.txt is NULL!!!");
return( 0 );
}
printf( "\nRead text!" );
for( record = 0 ; record < 10 ; record ++)
fscanf( fp , "%7s" , ch[ record ].txt );
for( conum = 0 ; conum < 10 ; conum ++ )
fscanf( fpp , "%7s" , ci[ conum ].txt );
for( record = 0 ; record < 10 ; record ++ )
{ch[ record ].Flag = 0;
for( conum = 0 ; conum < 10 ; conum ++ )
{
if( strcmp( ch[ record ].txt , ci[ conum ].txt ) != 0 )
ch[ record ].Flag = 1;
else
ch[ record ].Flag = 0;
}
}
for( record = 0 ; record < 10 ; record ++ )
printf( "\n%d" , ch[ record ].Flag );
}
----------------解决方案--------------------------------------------------------
没有那位高人可以指点一下迷津吗?
----------------解决方案--------------------------------------------------------
应该把else ch[recore].flag=0改为else {ch[recore].flag=0;break;}。
----------------解决方案--------------------------------------------------------
关键错误在这里: struct ch { char txt[ 7 ]; int Flag; }ch[ 10 ]; struct ci { char txt[ 7 ]; }ci[ 10 ]; 数组下标错了 应该是8 其它还有些小错误 正确的如下: #include<stdio.h> main() { FILE *fp,*fpp; int record,conum; struct ch { char txt[ 8 ]; int Flag; }ch[ 10 ]; struct ci { char txt[ 8 ]; }ci[ 10 ];
if((fp = fopen( "a.txt" , "r" ))== NULL ) {printf( "\na.txt is NULL!!!"); return( 0 );} if((fpp = fopen( "b.txt" ,"r" ))== NULL ) {printf( "\nb.txt is NULL!!!"); return( 0 );} for( record = 0 ; record < 10 ; record ++) {fscanf( fpp,"%s",ci[record].txt); fscanf( fp,"%s",ch[record].txt);} for( record = 0 ; *ch[record].txt; record ++ ) {ch[ record ].Flag = 0; for( conum = 0 ; *ci[conum].txt; conum ++ ) if( !strcmp( ch[ record ].txt , ci[ conum ].txt ) ) {ch[ record ].Flag = 1;break;} } for( record = 0 ; *ch[record].txt ; record ++ ) printf( "\n%d" , ch[ record ].Flag );
}
----------------解决方案--------------------------------------------------------