/* Write a program to print all input lines
* that are longer than 80 characters
*/
#include < stdio.h >
#define MINLENGTH 81
/*readbuff: read all input characters*/
int readbuff ( char *buffer )
{
size_t i = 0;
int c;
while( i < MINLENGTH )
{
c = getchar();
if ( c == EOF )
{
return -1;
}
if ( c == '\n' )
{
return 0;
}
buffer[i++] = c;
}
return 1;
}
/*copyread: copy the inputs that longer than 80 characters*/
int copyread ( char *buffer )
{
size_t i;
int c;
int status;
status = 1;
for ( i = 0; i < MINLENGTH; i++ )
{
putchar ( buffer[i++] );
}
while ( status == 1 )
{
c = getchar();
if ( c == EOF )
{
status = -1;
}
else if ( c == '\n' )
{
status = 0;
}
else
{
putchar(c);
}
putchar ( '\n' );
}
return status;
}
/*print the inputs that longer than 80 characters*/
main ( void )
{
int status;
char buffer[MINLENGTH];
/*initialize*/
status = 1;
while ( status != -1 )
{
status = readbuff ( buffer );
if ( status == 1 )
{
status = copyread ( buffer );
}
}
return 0;
}
C语言K&R习题系列——使用缓冲区函数接受长字符输入
最新推荐文章于 2023-07-25 11:11:37 发布