Re: Goto?!? (was Re: A89: fwrite bug located (and a bugfix))


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

Re: Goto?!? (was Re: A89: fwrite bug located (and a bugfix))




I don't know, but when I used the original version of fread, the data read
was garbled, but everything works fine with the version I did... Stupid me
didn't catch the unsigned warning (I'm a  IDIOT), but the code works. I
fixed it, and it doesn't use a goto at all. Well, try these two examples,
both use the fixed version of fwrite.

/*Using original fread*/

int _main()
{    
  unsigned int sprite[16] = {
  0xFFFF,0x8001,0x8001,0x8001,0x8001,0x8001,0x8001,0x8001,
  0x8001,0x8001,0x8001,0x8001,0x8001,0x8001,0x8001,0xFFFF};  
  unsigned int pic[16];
  FILE *fp;
    
  ClrScr ();
       
  fp = fopen ("example","wb");
  fwrite (sprite,32,1,fp);
  fputc (0,fp);
  fputs ("SPT",fp);
  fputc (0,fp);    
  fputc (OTH_TAG,fp);
  fclose (fp);
      
  fp = fopen ("example","rb");
  fread (pic,32,1,fp);
  fclose (fp);
            
  Sprite16 (5,5,16,pic,LCD_MEM,SPRT_XOR);
    
  ngetchx ();
  ClrScr ();
  return 0; 
}

This should display a 16x16 box with a 1 pixel wide border, but the sprite is garbled.

/*With new fread*/

#undef fread /*ignore stdio.h definition*/
unsigned fread(void *ptr,unsigned size,unsigned n,FILE *f)
{
  unsigned i,j;
  int saveflags=f->flags;
  f->flags|=_F_BIN;
  for(i=0;i<n;i++)
  for(j=0;j<size;j++)
  *(char*)ptr++=fgetc(f); /*seemingly doesn't matter whether you use a signed or unsigned char, did away with the goto statement entirely*/

  f->flags=saveflags;
  return i;
}

int _main()
{    
  unsigned int sprite[16] = {
  0xFFFF,0x8001,0x8001,0x8001,0x8001,0x8001,0x8001,0x8001,
  0x8001,0x8001,0x8001,0x8001,0x8001,0x8001,0x8001,0xFFFF};  
  unsigned int pic[16];
  FILE *fp;
    
  ClrScr ();
       
  fp = fopen ("example","wb");
  fwrite (sprite,32,1,fp);
  fputc (0,fp);
  fputs ("SPT",fp);
  fputc (0,fp);    
  fputc (OTH_TAG,fp);
  fclose (fp);
      
  fp = fopen ("example","rb");
  fread (pic,32,1,fp);
  fclose (fp);
            
  Sprite16 (5,5,16,pic,LCD_MEM,SPRT_XOR);
    
  ngetchx ();
  ClrScr ();
  return 0;

}

This correctly displays the sprite, so the originaly fread is buggy. I don't
know why it is, I just removed the test to determine if the value was
negative... oh well.

Michael Cowart

Michael Cowart




References: