编写一个程序,打印长度大于80的所有输入行。
这个很简单了,流程图如下:
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINE 1024
int getline(FILE *fin, char *line, size_t n);
int main(int argc, char argv[])
{
char currLine[MAXLINE] = "";
int currLen = 0;
FILE *fp = fopen("main.c", "r");
while ((currLen = getline(fp, currLine, MAXLINE-1)) != EOF) {
size_t len = 50;
if (currLen > len)
printf("%s", currLine);
}
return 0;
}
int getline(FILE *fin, char *line, size_t n)
{
int c = 0;
size_t i = 0;
while (i < n) {
c = fgetc(fin);
if (0 == i && EOF == c) return -1;
switch (c) {
case '\n':
{
line[i++] = '\n';
line[i++] = '\0';
return i;
}
case '\0':
case EOF:
{
line[i++] = c;
return i;
}
default:
line[i++] = c;
}
}
}