Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Worst programmer ever?

Name: Anonymous 2007-07-26 10:03 ID:4JryrsBt

So I wrote a function to copy a specified number of bytes
from one stream to another:

void vCopyFile(FILE *fIn, FILE *fOut, int iNum) {
    while (iNum--)
        (void)fputc(fgetc(fIn), fOut);
}

I was told the following:
20:34:53 < dinesh> wow
20:35:00 < dinesh> probably one of the worst function i ever saw

So I changed it to this:

void vCopyFile(FILE *fIn, FILE *fOut, int iNum) {
    char *cBuf = (char*)malloc(iNum);
    int iRead;

    iRead = fread(cBuf, 1, iNum, fIn);
    fwrite(cBuf, 1, iRead, fOut);
}

Both functions work on Linux but seem to skip bytes when
crosscompiled with mingw32-gcc or visual studio.

What am I doing wrong?

Name: Anonymous 2007-07-26 12:08 ID:DwqUjw26


#include <stdio.h>
#include <stddef.h>

int EXPERT_copy_file (FILE* in, FILE* out, size_t n)
{
    char buf[BUFSIZ];
    size_t nread;

    while (n > sizeof(buf)) {
        nread = fread(buf, 1, sizeof(buf), in);
        if (nread < sizeof(buf))
            return -1;
        if (fwrite(buf, 1, nread, out) < nread)
            return -1;
        /* lol rollback */
        n -= sizeof(buf);
    }
    nread = fread(buf, 1, n, in);
    if (nread < n)
        return -1;
    if (fwrite(buf, 1, nread, out) < nread)
        return -1;

    return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List