主函数中的第一个if语句出现了死循环,后面的就没法运行
主函数中的第一个if语句出现了死循环,后面的就没法运行,谢谢各位.问题在哪.
我是做ASP的,这是一个朋友的~~替他贴一下.
#include "stdio.h "
char *str[500];
char *s;
char filename[20];
char b[80];
int row;
main(argc,argv)
int argc;
char *argv[];
{ FILE *fp;
int i, j;
clrscr();
if( argc==1 )
{ printf( "Please input the name.\n ");
gets( filename );
}
if( argc==2 )
{ gets( filename );
strcpy( filename,argv[1] );
}
if( ( fp=fopen( filename, "r+ " ))==NULL )
{ printf( "Cannot open file.\n " );
exit (0);
}
for( i=0;i <500;i++ )
{ s=(char*)malloc( 128*sizeof(char) );
fgets( s,128,fp );
str[i]=s;
if ( feof(fp) ) break;
}
row=i+1;
printf( "Please input the commend:\n " );
gets( b );
read();
for( j=0;j <row;j++ )
puts( str[j] );
}
sign()
{ int p;
printf( "Please input the row:\n " );
scanf( "%d ",&p );
if( p> row ) p=row;
if( p <1 ) p=1;
return( p );
}
read()
{ int i, j, n, flag=0, c=1, l, t;
char s1[20], *s2[500], *s;
FILE *fp;
i=0;
while( b[i]!= '\0 ' )
i++;
for( j=i-1;b[j]!= ' ';j-- )
if( b[j]> = '0 '&&b[j] <= '9 ' )
{ flag+=1;
t=j-1;
}
l=sign();
if( flag==i-j-1 )
{ l=0;
for( i=t+1;b[i]!= '\0 ';i++ )
{ for( n=i;b[n]!= '\0 ';n++ )
c*=10;
c/=10;
l+=c*(b[i]-48);
c=1;
}
for( j=2;j <t;j++ )
s1[j-2]=b[j];
s1[j-2]= '\0 ';
}
else
{ for( i=2;b[i]!= '\0 ';i++ )
s1[i-2]=b[i];
}
if( ( fp=fopen( s1, "r " ) ) == NULL )
{ printf( "s1 open error.\n " );
exit( 0 );
}
for( i=0;i <500;i++ )
{ s=(char*)malloc( 128*sizeof( char ) );
fgets( s,128,fp );
s2[i]=s;
if( feof(fp) )
{ t=i;
break;
}
}
for( i=row-1;i> l-1;i-- )
str[i+t+1]=str[i];
for( i=l;i <=l+t+1;i++ )
str[i]=s2[i-l];
fclose( fp );
}
[解决办法]
错误很多啊
没有返回值类型,没有函数声明……
这个程序肯定没法运行,怎么知道在if死循环的啊
[解决办法]
错误还有:
内存泄露,分配的内存从来没有释放。
很多操作必须成对,生活中我们一般不会犯这样的错误,但编程时,很多人犯这样的错误,记得打开的文件需要关闭,分配的内存释放。
从冰箱取东西:打开冰箱,取东西,关闭冰箱。
洗手: 打开水龙头,洗手,关闭水龙头。
吃饭: 张开嘴, 放入食物, 闭嘴。
走路: 抬脚,迈步, 放下脚。