// Bad codewhile (fgets(buf, sizeof buf, instream)) {// What happens if there is no \n, buf[] is truncated leading to who knows whatbuf[strlen(buf) - 1] = '\0'; // attempt to rid trailing \n...}
If the final trailing '\n' is needed, alert the user to its absence and the action taken. IOWs, validate the file's format. Note: This may include a limit to the maximum line length, character encoding, etc.
Define clearly, document, the code's handling of a missing final '\n'.
Do not, as possible, generate a file the lacks the ending '\n'.