Re: A89: fwrite function
[Prev][Next][Index][Thread]
Re: A89: fwrite function
Hi!
> ...man I suck, I apologize for emailing this three times...
> but I think I have reviewed the code in source enough times
> to say that I think Zeljko overlooked a lot (or got tired of
> it) when he hacked together ANSI FILE support. The *crosses
> fingers* correct implementation should be like this, and
> similar check should be added to fputs:
> ...
Scott, I don't understand something (or you don't understand
something, or maybe both of us). Yes, __fwrite really does
not any memory checking, but if uses a pointer to function
(named fnc) which does actual storing a byte into the file.
If this function is implemented properly (with memory checking
and reallocating when necessary), everything will be good.
Now, look the implementation of fwrite later in stdio.h. It
calls __fwrite passing putc to it as the first argument. In
fact, it principally does
#define fwrite(p,s,n,f) __fwrite(fputc,p,s,n,f)
except __fwrite is constructed using a cast constructor. As
fwrite calls fputc (using a fnc parameter), and as fputc uses
memory checking and reallocating when necessary, fwrite will
work as expected. So, you are not right about incorrectness
of implementation of fwrite and fputs. Anyway, I am looking
for what is wrong with Michael's example.
Cheers,
Zeljko Juric
>
> unsigned __fwrite(int(*fnc)(int,FILE*),void *ptr,unsigned size,unsigned
> n,FILE *f)
> {
> unsigned i,j,free,needed;
> int saveflags=f->flags;
> char *base=f->base,*oldbase=base;
> if(f->flags&_F_ERR) return EOF;
> if(!(f->flags&_F_WRIT)) __FERROR(f);
> f->flags|=_F_BIN;
> if((needed = size*n) > (free = f->alloc - f->fpos - f->base))
> {
> 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;
> }
>
> Scott Dial wrote:
> >
> > 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
>
> --
> Scott "Wrath" Dial
> wrath@calc.org
> ICQ#3608935
> Member of TCPA - tcpa.calc.org
> PGP key available
>
> ____________NetZero Free Internet Access and Email_________
> Download Now http://www.netzero.net/download/index.html
> Request a CDROM 1-800-333-3633
> ___________________________________________________________
Follow-Ups: