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

Pages: 1-4041-

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 10:08 ID:4JryrsBt

Blerg. Forgot to copypasta "free(cBuf);".

Name: Anonymous 2007-07-26 10:18 ID:Heaven

you're an awful programmer.

Do you know what fgetc() returns if EOF is reached?
What if iNum is negative?
Do not cast your mallocs
If you're going to store the return value of fread, better error check it.


int vCopyFile(FILE *fp, FILE *dest, int nsize) {
    int c;
    int n = 0;
    if(fp == NULL || dest == NULL)
        return -1;
    for(c = getc(fp); c != EOF && n < nsize;
               n++, c = getc(fp))
        putc(c, fp);

    return n;
}

Name: Anonymous 2007-07-26 10:20 ID:Heaven

>>3 here
add to that you forgot to check the return value of malloc, and i lold @ the void cast

Name: Anonymous 2007-07-26 10:22 ID:HamNtPBW

Windows needs to be told it's in binary mode, try the _setmode function

Name: Anonymous 2007-07-26 10:25 ID:kmDLYctK

Yes, you possibly are the worst programmer, ever.

Your two functions go from one extreme to another. In one, you  process the stream one byte at a time, while in the other, you process the entire stream in one shot.

The first is unnecessarily slow. The second is unnecessarily wasteful on memory.

Also, just for fun, run your second function on a 5 GB stream and see what happens. You'll shit bricks when you see it.

Also, you fucking forgot to free the allocated memory like a jackass.

Also, reinventing the wheel, etc. library functions, motherfucker, do you use them?

/thread

Name: Anonymous 2007-07-26 10:28 ID:Heaven

>>6
Also, just for fun, run your second function on a 5 GB stream and see what happens. You'll shit bricks when you see it.
You mean on a 2+GB stream.

OP use open64() or O_LARGEFILE with open() and change the limit to a uint64_t

Name: Anonymous 2007-07-26 10:29 ID:Heaven

>>7
s/uint64/int64/

Name: Anonymous 2007-07-26 10:32 ID:kmDLYctK

>>3

Buffers, motherfucker, do you use them?

Name: Anonymous 2007-07-26 10:33 ID:Heaven

>>6
oh now i understood why you said a 5+GB stream
i though you where talking about the file IO operations, but apparently you where refering to the dynamic allocation part

Name: Anonymous 2007-07-26 10:36 ID:kmDLYctK

>>7

*facepalm*

Is this really the state of programming in the world today?

Have you still not seen the problem and shat bricks?

Name: Anonymous 2007-07-26 10:37 ID:kmDLYctK

>>11
>>10

Ok, you have shat the bricks. Carry on, then.

Name: Anonymous 2007-07-26 10:42 ID:Heaven

>>12
You want to shit moar bricks?

vCopyFile(fp, fp2, -1); /* cBuf = malloc(SIZE_MAX) */

Name: Anonymous 2007-07-26 10:44 ID:Enwuyh3j

Just tell him what's wrong.

Name: Anonymous 2007-07-26 10:45 ID:Heaven

>>14
We pointed every fucking thing that was wrong in his code you fucking idiot.

probably troll but these days it's hard to tell.

Name: Anonymous 2007-07-26 10:51 ID:HamNtPBW


int main(int argc, char *argv[])
{
    unsigned char buffer[1048576];
    int bytes_read= 0;
    int i;

#ifdef WINDOWS
    _setmode(_fileno(stdin), _O_BINARY);
    _setmode(_fileno(stdout), _O_BINARY);
#endif

    do {
        bytes_read = fread(buffer, 1, sizeof(buffer), stdin);
        /* TODO: insert code here to manipulate buffer, if needed */
        fwrite(buffer, bytes_read, 1, stdout);
    } while(bytes_read > 0 || !feof(stdin));

    return 0;
}

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

>>3
Thanks for your correction, but it still doesn't work.
After copying 264 bytes, the function returns 264 as it should,
but ftell() gives 265 for fp and 264 for dest.

I wish I were a troll, but in fact I just suck :/

Name: Anonymous 2007-07-26 10:57 ID:Heaven

unsigned char buffer[1048576];
fail
int bytes_read = 0;
fread returns size_t which is an object of unsigned type.
and why do you set it to 0?

        size_t bytes;
        while(bytes = fread(buffer, 1, sizeof(buffer), stdin)) {
            if(feof(stdin)) break;
            fwrite(buffer, bytes_read, 1, stdout);
        }

Name: Anonymous 2007-07-26 10:57 ID:Heaven

>>17
post your whole fucking code.

Name: Anonymous 2007-07-26 11:02 ID:f87SIMjE

>>1
Why do you beginners make such a mess with types, and, in particular, feel like you should throw a cast in every other line for good luck? Approximately 40% of the typecasts or type converstions I see in code I read are absolutely unnecessary.

P.S.: system("cp file1 file2");

Name: Anonymous 2007-07-26 11:08 ID:4JryrsBt

Name: Anonymous 2007-07-26 11:17 ID:ocs4RloG

Bastardized Hungarian notation (or any Hungarian notation) makes me want to cry.

It's so unnecessary, ugly, hard to read, and reeks of the Win32 API.

Name: Anonymous 2007-07-26 11:18 ID:Z28fIwsy

you're doing it wrong. here's how you do it

Prelude Data.ByteString.Lazy> :t \f1 f2 n -> hGet f1 n >>= hPut f2
\f1 f2 n -> hGet f1 n >>= hPut f2 :: GHC.IOBase.Handle -> GHC.IOBase.Handle -> Int -> IO ()

Name: Anonymous 2007-07-26 11:20 ID:Heaven

>>21
ololol

Name: Anonymous 2007-07-26 11:27 ID:HamNtPBW

>>18

You fail it, because the range that fread will return in that program (0 to 1048576) is well within signed or unsigned limits of an int.

Name: Anonymous 2007-07-26 11:31 ID:Heaven

>>25
*facepalm*
An object type of signed int is guaranteed to hold a value of max 32767 or more.

you're the one who fails at it, don't make me bring up the angered expert programmer copypasta.

Name: Anonymous 2007-07-26 11:32 ID:f87SIMjE

you're doing it wrong. here's how you do it

>>> from shutil import copyfileobj

Name: Anonymous 2007-07-26 11:33 ID:4JryrsBt

So do you have any suggestion that actually works? :X

Name: Anonymous 2007-07-26 11:34 ID:TKJQ6zBV

void copy(InputStream in, OutputStream out, int num, int bufSize) throws IOException {
    byte[] buf = new byte[bufSize];
    int n;
    while (num > 0 && (n = in.read(buf, 0, num < bufSize ? num : bufSize)) >= 0) {
        out.write(buf, 0, n);
        num -= n;
    }
}

Name: Anonymous 2007-07-26 11:36 ID:4JryrsBt

>>29
Fail for wrong language.

Name: Anonymous 2007-07-26 11:36 ID:HamNtPBW

>>26

You are such a 16-bit faggot. Get with the times.

Name: Anonymous 2007-07-26 11:37 ID:Heaven

>>26
That's the max of a 16 bit integer, you dumbass.

Name: Anonymous 2007-07-26 11:37 ID:Heaven

>>29
WHAT A FUCKIGN IDIOT
NOW THAT DESERVES TEH ANGERED EXPERT PROGRAMMER COPYPASTA
... buf = new ...
I SEE NO del buf IN YOUR CODE, NO I DON'T.
AND YOUR CODE FUCKING SUCKS ANYWAY, YOU DON'T CHECK WHAT new RETURNS AND ... OH MY GOD YOU KNOW WHAT? FUCK THAT. YOUR CODE IS OVERBLOATED AND VERY RETARDED.
I MEAN IT. VERY.

Name: Anonymous 2007-07-26 11:39 ID:Heaven

>>32
NO THAT'S WHAT THE STANDARD SAYS YOU FUCKING FUCKMIGN FUCKING FUKCIGN IDIOT


FUCK YOU ALL
FUCKIGN IDIOTS FUCKING FUCKTARDS FUCK

Name: Anonymous 2007-07-26 11:46 ID:HamNtPBW

>>34

I bet you're the type of guy who wanks off to prime-numbered RFCs every night.

Name: Anonymous 2007-07-26 11:48 ID:Heaven

>>35
WRONG FAGGOT, I'M THE TYPE OF GUY THAT WRITES PRIME-NUMBERED RFCs THAT TYPE OF GUYS YOU BET WANK OFF TO EVERY NIGHT.

Name: Anonymous 2007-07-26 11:51 ID:4JryrsBt

Looks like you're done insulting each other.

Maybe now someone could help?

Name: Anonymous 2007-07-26 11:51 ID:kmDLYctK

>>33

Oh god, the failure.

I do hope that you are a troll.

Name: Anonymous 2007-07-26 11:52 ID:kmDLYctK

>>37

We aren't doing your homework for you. We only come here to laugh at idiots. Good day.

Name: Anonymous 2007-07-26 11:52 ID:Heaven

>>37
Okay, I'll help.

Protip: stop programming.

Thread over.

Name: Anonymous 2007-07-26 11:52 ID:xXKsepsE

>>35
I bet you're the type of guy who wanks off to prime-numbered RFCs every night.

FUCKING LOL'D

Name: Anonymous 2007-07-26 11:56 ID:Heaven

>>38
explain or gtfo

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;
}

Name: Anonymous 2007-07-26 12:11 ID:Heaven

>>43
if (nread < sizeof(buf))
return -1;

fail, check for ferror.

dude, i never knew we have so many C n00bs here.

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

>>44
This is the EXPERT version, only to be compiled with the finest optimization flags. Requesting more than available is not tolerated.

Name: Anonymous 2007-07-26 12:25 ID:Heaven

>>45
you just dont get it eh?
if the size of the file is less than BUFSIZ or not a multiply of BUFSIZ your function will fail.
maybe in v2 man.

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

>>46
And what happens after the while loop?

Name: Anonymous 2007-07-26 12:43 ID:SAigVo1L

int EXPERT_copy_file(FILE *i,FILE *o,size_t n){
    char buf[0x400];
    size_t read,wrote,to_read=sizeof(buf);
    if(to_read<n)
        to_read=n;
   
    while((read=fread(buf,1,to_read,i))){
        if(read!=to_read)
            return -1;
        if ((wrote=fwrite(buf,1,read,o))!=read)
            return -1;
        n-=read;
        if(to_read<n)
            to_read=n;
    }
    return 0;
}

Name: Anonymous 2007-07-26 12:44 ID:SAigVo1L

Also, op is an obvious troll

Name: Anonymous 2007-07-26 12:48 ID:Heaven

>>47
WHAT HAPPENDS AFTER THE while LOOP? YOU STILL DO THE IF CHECK AND return -1 YOU FAGGOT
SAGE

Name: Anonymous 2007-07-26 13:17 ID:4JryrsBt

This thread needs moar flaming.

Name: Anonymous 2007-07-26 13:23 ID:Heaven

lol c
lisp is perfect solution for your problem.
I would know, i read SICP

Name: Anonymous 2007-07-26 13:23 ID:Heaven

>>51
NO U FUCKING IDIOT

Name: Anonymous 2007-07-26 14:50 ID:zbilUcPs

lol bump

Name: Anonymous 2007-07-26 14:52 ID:C8cMOUmF

I'M A GAY WALRUS !!!

Name: Anonymous 2007-07-26 16:12 ID:3c8igOW3

This is the funniest thread I've read for ages.

Masturbating to prime-numbered RFCs, indeed!

That makes me think, what we really need is a BBCode RFC. I would crack one off to that thrice daily.

Name: Anonymous 2007-07-26 18:20 ID:f87SIMjE

You are all such stupid faggots. You just can't get one thread right, do you? Stupid pieces of shits, I came from Reddit after some interesting articles but this is just too much; I've been reading the board a bit and it's all pages and pages of bullshit, you fucking faggots I'm outta here.

Name: Anonymous 2007-07-26 20:03 ID:Heaven

I'm outta here.
oh good :D

Name: Anonymous 2011-05-26 11:16

I fucking hate you,
shiitchan,
you piece of shit

Name: Anonymous 2011-05-26 11:17

[#]shiitchan,
you suck,
and I hate you[/#]

Name: Anonymous 2011-05-26 11:18

fucking shit

Name: Anonymous 2011-05-26 11:18

abc
def
ghi

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