1-17 over80.c

わりとad-hoc。

/*
 * P38 演習1-17
 *   80字より長い行を全て印字するプログラムを書け。
 *
 *         2007/05/24 arikui
 */

#include <stdio.h>

#define LINE_PUT_BORDER 80


//limitに達しない : > 0
//limitまで読んだ : 0
//EOF             : < 0
int read_line_part(char *buff, int limit);

void flash_rest_line(void);


int main(void){
    char buff[LINE_PUT_BORDER + 1];
    int result;
    
    
    while((result = read_line_part(buff, LINE_PUT_BORDER)) >= 0){
        if (result == 0){
            printf("%s", buff);
            flash_rest_line();
        }
    }
    
    
    return 0;
}



int read_line_part(char *buff, int limit){
    int c, len = 0;
    while(1){
        if (len >= limit) break;
        c = getchar();
        if (c == EOF) break;
        buff[len++] = c;
        if (c == '\n') break;
    }
    buff[len] = '\0';
    
    return (c == EOF && len == 0) ? -1 : (limit - len);
}


void flash_rest_line(void){
    int c;
    while((c = getchar()) != EOF){
        putchar(c);
        if (c == '\n') break;
    }
}