From owner-freebsd-hackers Mon Nov 22 15:45:45 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from hub.freebsd.org (localhost [127.0.0.1]) by hub.freebsd.org (Postfix) with ESMTP id 9229314DEA for ; Mon, 22 Nov 1999 15:45:40 -0800 (PST) (envelope-from green@FreeBSD.org) Received: from localhost (green@localhost [127.0.0.1]) by hub.freebsd.org (8.9.3/8.9.3) with ESMTP id SAA39880; Mon, 22 Nov 1999 18:42:32 -0500 (EST) (envelope-from green@FreeBSD.org) Date: Mon, 22 Nov 1999 18:42:32 -0500 (EST) From: Brian Fundakowski Feldman X-Sender: green@green.myip.org To: Zhihui Zhang Cc: freebsd-hackers@FreeBSD.org Subject: Re: A file with holes - a bug? In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Mon, 22 Nov 1999, Zhihui Zhang wrote: > > Please take a look at the following piece of code that creates a large > hole in a file named hole.dat. It tries to write 0x30-0x39 both at the > front and the tail of that file, the hole is therefore in the middle. > > main() > { > char c; > FILE * fp; > > fp = fopen("hole.dat", "w"); > > for (c=0x30; c<0x3a; c++) fputc(c, fp); > fputc('\n',fp); > fflush(fp); /* XXX */ > lseek(fileno(fp), 3 * 8192, SEEK_CUR); This should be fseek() and not lseek(). > for (c=0x30; c<0x3a; c++) fputc(c, fp); > fputc('\n',fp); > fclose(fp); > } > > If I remove the fflush(fp), then the characters 0x30-0x39 will be all > written at the end of the file (use hexdump to find out), not as expected > (one at the beginning and the other at the end). It seems to me that the > first for loop happens AFTER the lseek() statement without fflush(). Can > anyone explain this to me? I am using FreeBSD 3.3-Release. That's because you're not using fseek() like your should be using for FILE * IO. Don't mix FILE *fp and int fd operations callously. > > By the way, I also find out if you copy a file with holes into another > file, the holes in the first file will be replaced with 0s in the second > file, taking more disk space (check with du). Is there a better solution > for this? > > Any help is appreciated. > > -Zhihui > -- Brian Fundakowski Feldman \ FreeBSD: The Power to Serve! / green@FreeBSD.org `------------------------------' To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message