delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2001/10/21/18:57:22

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT sources DOT redhat DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sources DOT redhat DOT com>
List-Help: <mailto:cygwin-help AT sources DOT redhat DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT sources DOT redhat DOT com
Delivered-To: mailing list cygwin AT sources DOT redhat DOT com
Message-ID: <20011021225629.17478.qmail@foolabs.com>
Date: Sun, 21 Oct 2001 15:56:29 -0700 (PDT)
From: "Derek B. Noonburg" <derekn AT foolabs DOT com>
Subject: 1.3.3-2: fseek fails on multiples of 1024 (binary mode)
To: cygwin AT cygwin DOT com
MIME-Version: 1.0

The fseek function appears to behave strangely when both the offset and
file size are multiples of 1024.  This is with binary mode files -- I'm
calling fopen with "rb", and my test file has no control characters (CR,
LF, ^Z) at all.

I'm attaching a test program that should read and print eight bytes at
offset 1024 from a file called "foo" (after doing another fread and
fseek first - these are necessary to trigger the problem).

I used a file "foo" with 1024 'x's, then "abc", then 1021 'y's.  So the
file is exactly 2048 bytes long, and the program should print
"abcyyyyy".  Except that it prints "xxxxxxxx", apparently reading from
offset 0 instead of 1024.

If I call rewind before calling fseek (this is commented out in the
attached code), it appears to work around the bug.

I'm using the latest version of cygwin as of this afternoon (DLL
1.3.3-2, gcc 2.95.3-5), on Windows 98.

- Derek

#include <stdio.h>

int main() {
  FILE *f;
  char buf[1025];

  if (!(f = fopen("foo", "rb"))) {
    fprintf(stderr, "Couldn't open 'foo'\n");
    exit(1);
  }

  fread(buf, 1, 1024, f);

  fseek(f, 0, SEEK_END);
  printf("size = %ld\n", ftell(f));

#if 0
  rewind(f);
#endif
  fseek(f, 1024, SEEK_SET);
  printf("pos = %ld\n", ftell(f));
  fread(buf, 1, 8, f);
  buf[8] ='\0';
  printf("data = '%s'\n", buf);

  fclose(f);

  return 0;
}



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


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