Bug 000323

When Created: 08/30/2000 04:13:20
Against DJGPP version: 2.03
By whom:
Abstract: fopen/fseek/fwrite incorrect
When a file is fopen()'d for "ab" or "a+b", all fwrite() operations should
write at the end of the file, regardless of any fseek()'s. 

djdev 2.03 fwrite(), however, writes in the middle of the file if positioned 
by an fseek(), which is wrong.

I checked two Microsoft compilers on PC and the Linux GCC 2.8.1, they work
correctly in this respect.

Attached are:
ab.c : an example program that produces the bug
ab.bin : an example file that is modified incorrectly by ab.exe at offset 5

To produce the executable:
gcc -o ab.exe ab.c

The ident of the executable is:
     $Id: stub.asm built 12/09/99 10:59:31 by djasm $
     $Id: DJGPP libc built Jan  4 2000 14:33:38 by gcc 2.8.1 $

---------------------------------- ab.c -----------------------------------
#include <stdio.h>

int main( void )
FILE    *f;
char    c = 'X';

    if (f = fopen( "ab.bin", "ab" ))
        fseek( f, 5, SEEK_SET ); 
        fwrite( &c, 1, 1, f );   /* should append X at the end, but bumps it at offset 5 */
        fclose( f );
    return 0;
---------------------------End of ab.c ------------------------------------

--------------------------------- ab.bin ----------------------------------
----------------------------End of ab.bin ---------------------------------

Note added: 02/18/2001 11:15:07
By whom:
Using DjDevkit 2.01 for MS-Dos

There's another trouble with "fwrite": in some cases, i pastes some zeroes after the datas i want to write in the file.

fwrite 16 bytes: ok
fwrite 48 bytes: ok
fwrite 336 bytes: returns '336 bytes wrote' but actually, if i close the file here, i have 6 exceeding bytes after the 'good' ones :(

Note added: 02/19/2001 06:24:46
By whom:
The above note has nothing to do with the original bug report.

Besides, I'm sure it's a user error, because fwrite doesn't add
any extra bytes.  Unfortunately, the user didn't present any sources...

Note added: 10/14/2002 15:52:44
By whom:
I tried to reproduce the problem described in the note.
I could not reproduce it. Below is the test program
I used:

---Start test program---
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

main (void)
  FILE   *f = NULL;
  char    buf[337];
  size_t  sz = 0;

  memset(buf, 0, sizeof(buf));

  f = fopen("ab-2.bin", "ab");
  if (f)
      sz = 16;
      assert(fwrite(buf, sz, 1, f) == 1);
      sz = 48;
      assert(fwrite(buf, sz, 1, f) == 1);
      sz = 336;
      assert(fwrite(buf, sz, 1, f) == 1);

---End test program---

I got a 400-byte file "ab-2.bin", when I built this
against DJGPP 2.03 and DJGPP CVS (which will be 2.04).

I think the user may be confused: fwrite() does not
return the number of bytes written; it returns the number
of objects written.

Solution added: 10/14/2002 15:56:16
By whom:
This bug is fixed in DJGPP CVS (DJGPP 2.04).
The test program works as the user expects,
when built with DJGPP CVS (DJGPP 2.04).

Fixed in version cvs on 10/14/2002 15:57:10
By whom:

  webmaster     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010