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

Newsgroups: comp.os.msdos.djgpp
From: fredex AT fcshome DOT stoneham DOT ma DOT us (Fred Smith)
Subject: Re: DJGPP BUG ?
Organization: None!
Message-ID: <E8txA5.268@fcshome.stoneham.ma.us>
References: <5j5n1j$lph AT news DOT network DOT com>
Date: Fri, 18 Apr 1997 10:32:28 GMT
Lines: 56
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


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