Date: Fri, 18 Apr 1997 06:42:17 +0300 (EET DST) From: Samuli Takala To: djgpp AT delorie DOT com Subject: Re: DJGPP BUG ? In-Reply-To: <5j5n1j$lph@news.network.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On 17 Apr 1997, Mike Collins wrote: > Am I doing something wrong? Why does the following not work? > #include > #include > > main() > { FILE *fp; > fp = fopen("junk", "wb"); > fwrite("abcdefghijklmnopqrstuvwxyz", 26, 1, fp); > // examination of the file at this point shows that it > // contains 26 characters of the alphabet. To this point, > // it works When I try to examine the file here, it contains 0 bytes and is inaccessible (under Win95). > printf("%d", (int)(filelength(fileno(fp)))); // prints "0" > // - WHY? File output in djgpp is buffered. Nothing is really written to the file until the file is closed, or fflush(), fseek() or rewind() is called. So you probably want to add the following line before the printf(): fflush(fp); > fclose(fp); > fp = fopen("junk", "rb+"); > printf("%d", (int)(filelength(fileno(fp)))); > // prints 26 as expected Here should be something like: fseek(fp,0,SEEK_END); > fwrite("abcdefghijklmnopqrstuvwxyz", 26, 1, fp); > // examination of the file at this point shows that it > // contains 52 characters of the alphabet - Fine! > > printf("%d", (int)(filelength(fileno(fp)))); > // still prints 26! > } Again, nothing is written to the file until the file is closed. > In my application, a file is written to, then its handle is > passed to a function which needs to know the length of the file. > This function cannot close and reopen the file, however, because > it does not know the filename, which is derived in the first > place by a fairly complex process (reading bits of other files) > I don't want to have to go through developing the name of the > file in the called function. Just call fflush(fp) just before getting the file length and it should work. > This works fine under my 16-bit DOS-based compiler (Power-C by > MIX). > > Another thing that works under Power-C but did not under DJGPP > was to do with trunkation of a file using chsize(). The > returned value was different if I closed the file with > fclose(fp) (returned zero) or with close(fileno(fp)) (returned > something else, but I can't remember what), but the trunkation > didn't work in either case. I finally solved it by opening a > second file, copying the first file into it up to the trunkation > point, then closing both, deleting the original and renaming the > new file to the original's name, and that works - but it's > cumbersome! From the DJGPP C Library reference: chsize Syntax #include int chsize(int handle, long size); Description Just calls ftruncate (*note ftruncate::.). Return Value Zero on success, -1 on failure. ftruncate Syntax #include int ftruncate(int file, off_t where); Description This function truncates FILE at WHERE length. This only works if the file is closed right after this call. Return Value Zero for success, nonzero for failure. So, you'll have to close the file immediately after the truncation. I tested it, and it worked fine. Hope this helps, Samppa Samuli Takala Samuli DOT Takala AT hut DOT fi finger -l tax AT hut DOT fi for long version