Mail Archives: djgpp/1997/04/20/23:53:25
Mike Collins (e-mail DOT address AT end DOT of DOT text) wrote:
: Am I doing something wrong?
Yes.
: Why does the following not work?
Try doing:
fflush (fp);
BEFORE the filelength call.
: 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.
The file is not (necessarily) actually written to disk until
it is either fclose()'d, or fflush()'d. I believe the behavior
you see is therefore valid.
: 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.
Use fflush(). That's what it's for. See above.
: 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!
It sounds as if you're trying to mix stdio file management functions
and low-level (i.e., open/read/write/close) functions. This is generally
risky territory (thin ice) unless you are VERY knowledgeable and
careful about what you are doing. I'd suggest that if you are using
fopen/fclose/fread/fwrite to manage a file, then you want to use chsize
on it, that you (at least) make sure to fflush() it before the
chsize, better yet, close and re-open it first.
Fred
--
-------------------------------------------------------------------------------
.---- Fred Smith / Office: fred AT computrition DOT com
( /__ ,__. __ __ / __ : / 508-663-2524
/ / / /__) / / /__) .+' Home: fredex AT fcshome DOT stoneham DOT ma DOT us
/ / (__ (___ (__(_ (___ / :__ 617-438-5471
-------------------------------- Jude 1:24,25 ---------------------------------
- Raw text -