0
votes

I am using fgets to read a file line by line

    char buffer[4096];

    while (fgets(buffer, sizeof(buffer), file__1) != NULL) {
           
           fprintf(file__2, "%s", buffer);
    }

However the man page of fgets() says this under the Examples section

           while (fgets(line, line_max + 1, fp) != NULL) {
               // Verify that a full line has been read ...
               // If not, report an error or prepare to treat the
               // next time through the loop as a read of a
               // continuation of the current line.
               ...
               // Process line ...
               ...
           }

My question is, upon fgets failing how could i "Verify that a full line has been read"?

See if the last character of the string is a newline. - Shawn
(And don't forget the case where the file doesn't have a newline as its last character, which unfortunately happens on occasion) - Shawn
If you can use it, prefer POSIX getline(3), which doesn't have this issue because it reallocates the line buffer if needed. - Shawn