Well you can replace this
result = fgets(s, n, fp); /* read a line */

With a loop which looks a lot like this (which you already have later)
while ((ch = getc(fp)) != '\n' && (ch !=...