delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/11/15/06:34:08

Message-ID: <3A12736A.CF611454@example.com>
From: Nobody <reply-to-newsgroup AT example DOT com>
X-Mailer: Mozilla 4.75 [en] (X11; U; Linux 2.2.17 i686)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: possible fflush bug?
Lines: 56
Date: Wed, 15 Nov 2000 05:28:42 -0600
NNTP-Posting-Host: 216.80.208.18
X-Complaints-To: abuse AT onemain DOT com
X-Trace: nntp1.onemain.com 974287553 216.80.208.18 (Wed, 15 Nov 2000 06:25:53 EST)
NNTP-Posting-Date: Wed, 15 Nov 2000 06:25:53 EST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I think I found a bug in fflush. I am using gcc2952b, djdev203b, and
bnu281b. After creating a read/write binary file, writing to it, doing
an absolute seek backwards, reading, and calling fflush on the FILE*,
the file position as reported by ftell jumps to the end of the file;
trying to read from the FILE* fails, and feof returns true.

I ran into this bug in a larger program and created the demonstration
below based on that.

The demonstration code below prints the following with the above
packages:
pos after fwrite: 4
pos after fseek to 2: 2
pos after fgetc: 3
pos after fflush: 4

It prints this under Linux with RedHat's glibc-2.1.94-3, egcs-1.1.2-30,
and binutils-2.10.0.18-1:
pos after fwrite: 4
pos after fseek to 2: 2
pos after fgetc: 3
pos after fflush: 3

Workaround:
fseek(fp, 0, SEEK_CUR); or fseek(fp, ftell(fp), SEEK_SET); before
fflush(fp);

To demonstrate:
#include <stdio.h>

int main(void) {
  FILE *fp;
  int c;

  fp = fopen("foo", "w+b");

  fwrite("foo\n", 1, 4, fp);

  printf("pos after fwrite: %ld\n", ftell(fp));

  fseek(fp, 2, SEEK_SET);

  printf("pos after fseek to 2: %ld\n", ftell(fp));

  c = fgetc(fp);

  printf("pos after fgetc: %ld\n", ftell(fp));

  fflush(fp);

  printf("pos after fflush: %ld\n", ftell(fp));

  fclose(fp);

  return 0;
}

- Raw text -


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