This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

1.3.3-2: fseek fails on multiples of 1024 (binary mode)


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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]