#include <stdio.h>
#define MAXLINE 81
#define MORE 1
#define NOMORE 0
int mygetline(char line[], int maxline);
int main(void) {
int tmplen, len, max;
int state;
char line[MAXLINE];
tmplen = len = max = 0;
/* init state is NOMORE */
state = NOMORE;
while ((tmplen = mygetline(line, MAXLINE)) > 0) {
/* MORE line */
if (tmplen == MAXLINE - 1 && line[MAXLINE - 2] != '\n') {
len += tmplen;
state = MORE;
printf("%s", line);
} else { /* NOMORE line */
if (state == MORE) {
len += tmplen;
line[tmplen - 1] = '\0';
printf("%s [%d]\n", line, len);
}
else
len = tmplen;
if (len > max)
max = len;
len = 0;
state = NOMORE;
}
}
return 0;
}
int mygetline(char line[], int lim) {
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
line[i] = c;
if (c == '\n')
line[i++] = c;
line[i] = '\0';
return i;
}
类似于1-16题,只是修改了MAXLINE为81,设置缓冲区数据大小为81个字符(缓冲有一个字符存放'\0'),再选择输出。
主函数中,/*MORE LINE*/中代码判断是否大于80字符,满足输出,/*NOMORE LINE*/中不输出(如果不是未完行的结尾),否则输出。效果就是输出大于80字符的行。