Help with use of goto

i agree the posted example did not illustrate my point well.

the following is somewhat better, illustrating some post processing that needs to be performed regardless of whether the code completes successfully or if there are errors.

in other cases, again requiring some post clean-up, a goto simplifies the coded if an error occurs within conditional tests within loops

void
cfgSave (
    const char *filename )
{
    printf ("%s: %s\n", __func__, filename);

    FILE * fp = fopen (filename, "wb+");
    if (NULL == fp)  {
        perror ("cfgSave - fopen");
        return;
    }

    if (1 != fwrite ((void*) cfgMagic, sizeof(cfgMagic), 1, fp)) {
        perror ("cfgSave: fwrite cfgMagic");
        goto done;
    }

    CfgHdr_s  hdr;
    for (CfgVar_s *p = cfgVarTbl; V_NONE != p->type; p++)  {
        hdr.id   = p->id;
        hdr.size = p->nByte;

        if (1 != fwrite ((void*) & hdr, sizeof(hdr), 1, fp)) {
            perror ("cfgSave: fwrite hdr");
            goto done;
        }

        int nwr = fwrite ((void*) p->p, p->nByte, 1, fp);
        if (! nwr)  {
            printf (" %s: fwrite incomplete, %d %d",
                        __func__, nwr, p->nByte);
            perror (" cfgSave - fwrite");
            goto done;
        }
    }

done:
    fclose (fp);
}

i understand that it is hard to see that value of a goto in this very limited case until you've found a need

it's sad for developers to ignore this valuable tool