1

Does the ferror in this example check check both fprintfs for error, or just the second one?

FILE * myout;
if ((myout = fopen("Assignment 11.txt", "a")) != NULL)
{
    fprintf(myout, "First print ", str1);  
    fprintf(myout, "Second print", str1);

    if (ferror(myout))
        fprintf(stderr, "Error printing to file!");

    fclose(myout);
}
Cullub
  • 2,901
  • 3
  • 30
  • 47
  • I would think you would want to check after each write so that you know exactly when an error occurred. – Michael Dorgan Nov 29 '16 at 21:52
  • @Michael Yes, maybe, in a perfect world :) But currently, I'm printing two times right next to each other; basically two parts of the same line of text. I don't really care which one makes the error, I just need to know whether any errors occurred. – Cullub Nov 29 '16 at 21:55

1 Answers1

2

If an error occurs, it won't be reset unless clearerr is called on your stream, so yes, an error occuring on any of both writes is recorded.

from ferror manual page:

The function ferror() tests the error indicator for the stream pointed to by stream, returning nonzero if it is set. The error indicator can only be reset by the clearerr() function.

But you could also simply use fprintf return code to see if something went wrong:

If an output error is encountered, a negative value is returned.

(fprintf manual page)

Like this (Thanks Jonathan for pointing out the errors in the original post):

if (fprintf(myout, "First print %s\n", str1)<0) fprintf(stderr, "Error printing to file #1!");
if (fprintf(myout, "Second print %s\n", str1)<0) fprintf(stderr, "Error printing to file #2!");
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Can you verify that this is standard C behavior - not Linux specific? – Cullub Nov 29 '16 at 22:05
  • 3
    See POSIX: [`ferror()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/ferror.html) and [`fprint()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprint.html) and [`clearerr()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/clearerr.html). Or find the C standard online, but POSIX doesn't go against the C standard where it can avoid it. – Jonathan Leffler Nov 29 '16 at 22:07
  • 1
    @JonathanLeffler: POSIX is much more readable/accessible than the C standard, and for standard C functions and headers, POSIX requirements beyond what ISO C requires are usually "shaded" (tagged) with the "CX" marker in the POSIX versions of their specifications. – R.. GitHub STOP HELPING ICE Nov 29 '16 at 22:52
  • The claim of that Linux manpage that "The error indicator can only be reset by the clearerr() function" is false, as [rewind()](https://linux.die.net/man/3/rewind), at least, also clears it. The manpage also claims that the end-of-file indicator can only be reset by clearerr(), which is even more wrong since even fseek() clears that one. The C and POSIX standards seem to make no guarantee that other functions don't reset the error indicator, and I don't trust the Linux manpages since they aren't even internally consistent. – benrg Dec 16 '20 at 22:43