delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/20/10:23:04

Date: Sun, 20 Apr 1997 17:06:57 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Mike Collins <e-mail DOT address AT end DOT of DOT text>
cc: djgpp AT delorie DOT com
Subject: Re: DJGPP BUG ?
In-Reply-To: <5j5n1j$lph@news.network.com>
Message-ID: <Pine.SUN.3.91.970420170636.578N-100000@is>
MIME-Version: 1.0

On 17 Apr 1997, Mike Collins wrote:

> 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.

One way is just to call `ftell'.  It returns a byte position of the
last byte written to the file, which is exactly what you need.

If you insist on calling `filelength', you will need to force the file
data to be actually delivered to disk, or else the return value will
never be correct, as you have discovered.  There are 3 ways that I'm
aware of to do that.  One is to close the file.  The other 2 are:

	1) Call `fflush' and then `fsync'.  This usually has the
effect of flushing the data to DOS and forcing DOS to write buffered
data to the disk.  Note that `fflush' alone is NOT enough!

	2) Duplicate the file handle using `dup', then close the other
handle.  This has the same effect as `fclose', but you retain the
handle.

> 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))

Don't do that!  Don't *ever* call `close' on a file which was open
with `fopen', because `fopen' does a lot of additional bookkeeping
that needs to be undone by `fclose'.

> but the trunkation didn't work in either case.

I don't understand why did you even need to call `ftruncate'.  It is
seldom needed, because writing to a file truncates it at the point of
the last write.  Please tell more about your problem.

- Raw text -


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