www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/21/16:31:42

From: e-mail DOT address AT end DOT of DOT text (Mike Collins)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: DJGPP BUG ?
Date: 21 Apr 1997 17:22:25 GMT
Organization: Storage Technology Limited
Message-ID: <5jg7oh$3v3@news.network.com>
References: <Pine DOT OSF DOT 3 DOT 96 DOT 970418062103 DOT 11389A-100000 AT alpha DOT hut DOT fi>
NNTP-Posting-Host: 129.80.172.76
Mime-Version: 1.0
Lines: 105
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article 
<Pine DOT OSF DOT 3 DOT 96 DOT 970418062103 DOT 11389A-100000 AT alpha DOT hut DOT fi>, 
tax AT cc DOT hut DOT fi says...
>
>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.
>
>Just call fflush(fp) just before getting the file length and 
it should work.
>

Thanks, I'll do it. It's a simple enough solution!

>> 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 <io.h>
>     int chsize(int handle, long size);
>Description
>     Just calls ftruncate (*note ftruncate::.).
>Return Value
>     Zero on success, -1 on failure.
>
>ftruncate
>Syntax
>     #include <unistd.h>
>     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.
>

It's a bit of a nuicance, though. In my program, the file in 
question contains a list of structures. The file-management 
function calls a function that displays a screen-full of the 
structures, then checks the keyboard. If it detects the delete 
key, it calls a routine that deletes the structure 
(highlighted on the screen) and and copies all following 
structures into the slot before. Imagine the list of 
structures as 1 2 3 4 5 6 7 8, and we want to delete number 4. 
5 gets copied to the 4th position, 6 to the fifth, etc, and 8 
to the seventh, so we end up with 1 2 3 5 6 7 8 8. At this 
point, I want to trunkate the file to get rid of the extra 8. 
The trunkation should logically be part of the 'delete' 
routine, but it doesn't know the file name to re-open it, so I 
don't really want to have to close / re-open it. I haven't 
checked the C reference, but is this standard C - that 
chsize() must be followed with close()?  

I have never had to port a C program between compilers before, 
but my impression was that the language was so standard that 
it would be a trivial task. I have spent ages on doing it, and 
I'm not there yet. Some of the trouble was that my program was 
not in standard C (conio routines, etc) and some was because 
the original program was under DOS and used keywords like 
'huge' and 'far', but a lot of the trouble has been simply due 
to differences between the compilers.

I must confess to being a little disillusioned.

Thanks for the answer, though.

Mike.
-- 
Don't just hit "reply" - my E-mail address is bogus
to avoid automatic browsers from sending junk mail.
Please use collim'at'anubis'dot'network'dot'com

- Raw text -


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