Re: A89: fwrite function


[Prev][Next][Index][Thread]

Re: A89: fwrite function




I wanna make a correction to a mistake I made.... f->pos should be
f->fpos, note below the change

Scott Dial wrote:
> 
> This is my third time writing this email (don't ask), so this is the
> abrigded version of this email. fwrite is "buggy" or rather implemented
> badly. fwrite does no memory checking, thus does not allocate the space
> nessecary for the data being wrote. I suggest that the fwrite
> implementation be changed to something similar to this: (btw, fputs has
> a similar problem... which I don't have time to rewrite)
> 
> unsigned __fwrite(int(*fnc)(int,FILE*),void *ptr,unsigned size,unsigned
> n,FILE *f)
> {
>   unsigned i,j,free,needed;
>   int saveflags=f->flags;
>   f->flags|=_F_BIN;
    free = f->alloc - f->fpos - f->base;
>   needed = size*n;
>   if(needed>free)
>   {
>     HeapUnlock(f->handle);
>     if(!HeapRealloc(f->handle,f->alloc+=needed-free)) __FERROR(f);
>     base=f->base=HLock(f->handle);
>     f->fpos+=base-oldbase;
>     oldbase=base;
>   }
>   for(i=0;i<n;i++)
>     for(j=0;j<size;j++)
>       if(fnc(*(char*)ptr++,f)<0) goto exit;
> exit:
>   f->flags=saveflags;
>   return i;
> }
> 
> Michael Cowart wrote:
> >
> > Ok, what is wrong here?
> >
> > FILE *fp = fopen("example","wb");
> > fwrite (vplane0,3840,1,fp); /*vplane is a pointer to a 3840
> > byte screen buffer*/
> > fputc(0,fp);
> > fputs("PNT",fp);
> > fputc(0,fp);
> > fputc(OTH_TAG,fp);
> > fclose(fp);
> >
> > When I create this file, it's only 371 bytes long... Is fwrite buggy, or am
> > I just insane : ) ?
> >
> > Thanx,
> > Michael Cowart
> 
> --
> Scott "Wrath" Dial
> wrath@calc.org
> ICQ#3608935
> Member of TCPA - tcpa.calc.org
> PGP key available
> 
> _____NetZero Free Internet Access and Email______
>    http://www.netzero.net/download/index.html

-- 
Scott "Wrath" Dial
wrath@calc.org
ICQ#3608935
Member of TCPA - tcpa.calc.org
PGP key available
_______________________________________________
Why pay for something you could get for free?
NetZero provides FREE Internet Access and Email
http://www.netzero.net/download/index.html



Follow-Ups: References: