Примерно так:
// If we read an incomplete line
if(strlen(line) == 74 && line[73] != '\n') {
// Read until next newline
int ch; // Yes, should be int and not char
while((ch = fgetc(fp)) != EOF) {
if(ch == '\n')
break;
}
}
Поместите его после блока else.
Вот полная версия, которая исправляет распечатки правильно:
void checkLine( char const fileName[]){
FILE *fp = fopen(fileName,"r");
char line[75];
while (1) {
fgets(line,75,fp);
if (feof(fp)){
break;
} else {
// fgets stores the \n in the string unless ...
printf("%s", line);
}
if(strlen(line) == 74 && line[73] != '\n') {
// ... unless the string is too long
printf("\n");
int ch;
while((ch = fgetc(fp)) != EOF) {
if(ch == '\n')
break;
}
}
}
}
if(strlen(line) == 74 && line[73] != '\n')
можно заменить на if(strchr(line, '\n'))
, если вы предпочитаете.
И, конечно,, вы должны проверить возвращаемое значение для fgets
и fopen
в случае ошибки.