delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1998/06/26/15:19:21

Message-ID: <35940E1D.102CEB7@gmx.net>
Date: Fri, 26 Jun 1998 21:09:49 +0000
From: Robert Hoehne <robert DOT hoehne AT gmx DOT net>
Organization: none provided
MIME-Version: 1.0
To: djgpp-workers <djgpp-workers AT delorie DOT com>
Subject: Bug in fwrite

I found a bug in fwrite and I hope, I haven't missed
something and this is fixed already, but the "latest"
libc alpha from 1.1.98 has this also.

If someone has a bugfix this would be good, since I'm
writing currently program where I need this but haven't
all that detailed stdio knowlage to fix it the right way.

I think the best is, to use the following test program,
which is commented by me.

#include <stdio.h>

int main()
{
  int i;
  FILE *f;
  f = fopen("t.dat","w+b");
  i = 0;
  fwrite(&i, sizeof(i), 1, f);
  fwrite(&i, sizeof(i), 1, f);
  fseek(f, 0, 0);
  /* until here all OK, the buffer is flushed to the disk
     after the fseek */
  fread(&i, sizeof(i), 1, f);
  /* now the buffer is newly allocated because it was
     reset after the last fseek and because of the read,
     in the _flag (member of FILE) is the last operation
     set to read */
  i = -1;
  fwrite(&i, sizeof(i), 1, f);
  /* here comes now the bug. Since we write now, but the buffer
     is still big enough so we dont need to call _flsbuf() which
     would remember the write operation as the last in _flag. We do
     simply a memcpy here now in the buffer */
  fseek(f, sizeof(i), 0);
  /* since there is still the last operation as read marked in _flag,
     fseek will not flush the buffer to disk */
  fread(&i, sizeof(i), 1, f);
  /* and here is the result: we get 0 instead of -1 !!!! */
  fclose(f);
  return 1;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019