From owner-freebsd-hackers Thu Jan 4 16:51:20 2001 From owner-freebsd-hackers@FreeBSD.ORG Thu Jan 4 16:51:16 2001 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from jello.geekspace.com (jello.geekspace.com [63.175.99.14]) by hub.freebsd.org (Postfix) with SMTP id 79EA037B400 for ; Thu, 4 Jan 2001 16:51:15 -0800 (PST) Received: (qmail 39498 invoked from network); 5 Jan 2001 00:53:56 -0000 Received: from footrest.third-rail.net (HELO geekspace.com) (63.175.99.10) by jello.geekspace.com with SMTP; 5 Jan 2001 00:53:56 -0000 Message-ID: <3A551ABD.DBC9BD5C@geekspace.com> Date: Thu, 04 Jan 2001 19:52:13 -0500 From: Chris Williams X-Mailer: Mozilla 4.73 [en] (Windows NT 5.0; U) X-Accept-Language: en MIME-Version: 1.0 To: freebsd-hackers@freebsd.org Cc: rozzin@geekspace.com Subject: Strange fwrite() behavior in a+ mode Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG If I run the following program, I get the following results: --- jello:/usr/home/chris/foo$ cat blah.c #include int main() { FILE *f = fopen("foo", "a+"); char x[50] = " "; char y[50] = " "; fseek(f, 0, SEEK_SET); printf("tell: %d\n", ftell(f)); printf("fread return'd: %d\n", fread(x, (size_t) 1, (size_t)3, f)); printf("read: %s\n", x); printf("tell: %d\n", ftell(f)); fwrite("xxx", (size_t) 1, (size_t) 3, f); printf("tell: %d\n", ftell(f)); printf("fread return'd: %d\n", fread(y, (size_t) 1, (size_t)3, f)); printf("read: %s\n", y); printf("tell: %d\n", ftell(f)); } jello:/usr/home/chris/foo$ cat foo 123456789 jello:/usr/home/chris/foo$ cc blah.c -o bar;./bar tell: 0 fread return'd: 3 read: 123 tell: 3 tell: 13 fread return'd: 0 read: tell: 13 jello:/usr/home/chris/foo$ --- But, if I comment out the read before the write, I get somewhat strange behavior: --- jello:/usr/home/chris/foo$ cat blah.c #include int main() { FILE *f = fopen("foo", "a+"); char x[50] = " "; char y[50] = " "; fseek(f, 0, SEEK_SET); printf("tell: %d\n", ftell(f)); // printf("fread return'd: %d\n", fread(x, (size_t) 1, (size_t)3, f)); // printf("read: %s\n", x); printf("tell: %d\n", ftell(f)); fwrite("xxx", (size_t) 1, (size_t) 3, f); printf("tell: %d\n", ftell(f)); printf("fread return'd: %d\n", fread(y, (size_t) 1, (size_t)3, f)); printf("read: %s\n", y); printf("tell: %d\n", ftell(f)); } jello:/usr/home/chris/foo$ cat foo 123456789 jello:/usr/home/chris/foo$ cc blah.c -o bar;./bar tell: 0 tell: 0 tell: 3 fread return'd: 0 read: tell: 13 jello:/usr/home/chris/foo$ --- It tells me I'm at 3, but my read acts as if I'm at the end of the file, and then after trying to read it tells me I'm at 13 (the end). Is this a bug? Am I doing something illegal, and the behavior is random? Or..? According to Rozzin (CCed, who brought the issue to my attention), under Linux he sees the first behavior (move pointer to end of file, and tell you that) consistantly. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message