#栈溢出——实例#

#include<stdio.h>

const int PAGELINE = 24;
const int LINELEN = 512;

void do_more(FILE *fp);
int see_more();

int main(int ac, char * av[])
{

FILE *fp;
if(ac == 1)
{
do_more(stdin);
}
else
while( ac-- )
//printf("%s\n",av[1]);
if( (fp = fopen(*(++av),"r" )) != NULL )
{
do_more(fp);
fclose(fp);
}
else
{
exit(1);

}
return 0;
}

/* read Page_Line lines,the calls see_more() for further instructions */
void do_more(FILE *fp)
{

/**************************************************************************/

//数组过小导致读取19行时溢出,将num_of_lines值更改为异常大数值

/****************************************************************************/
char ch_line[PAGELINE];
int num_of_lines = 0;
//int see_more(), reply;
int reply;

while( fgets( ch_line, LINELEN, fp) )
{
if(num_of_lines == PAGELINE)
{
printf("see_more\n");
reply = see_more();
if( reply == 0)
{ break; }

num_of_lines = 0;
}

if( fputs( ch_line,stdout ) == EOF )
{
printf("exit\n");
exit(1) ;
}

printf("\n");
printf("lines = [%d]\n",num_of_lines);
num_of_lines++;
}
}

int see_more()
{
int c;
printf("\033[7m more? \033[m");
while( (c = getchar()) != EOF )
{
if( c == 'q')
{
return 0;
}
else if( c == ' ')
{
return PAGELINE;
}
else if( c == '\n')
{
return 1;
}
}

return 0;
}

posted @ 2020-08-21 16:02  水水$88  阅读(196)  评论(0)    收藏  举报