Search code examples
cwidechar

When does fwprintf return a negative number?


I couldn't find it in the docs anywhere, all I did find was that it returns a negative number when an error occurs. What error can it be?

The error occurs in a function that looks like this:

void foo(wchar_t** a)
{
    for (int i = 0; i < N; i++)
        if (fwprintf(stderr, L"%ls ", a[i]) < 0)
            puts("OOPS!");
    fwprintf(stderr, L"\n");
}

Also, this error appears only at one point during the program execution and stays there, as if some kind of limit was reached.


Solution

  • According to the definition of fwprintf:

    Return Value On success, the total number of characters written is returned.

    If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.

    If a multibyte character encoding error occurs while writing wide characters, errno is set to EILSEQ and a negative number is returned.

    So it is either returning a negative number because of a writing error or a multibyte character encoding error. Since you are using a wide char double pointer it is most likely the latter, so check the errno to see if it is set to EILSEQ.