delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/17/23:57:35

Date: Fri, 18 Apr 1997 06:42:17 +0300 (EET DST)
From: Samuli Takala <tax AT cc DOT hut DOT fi>
To: djgpp AT delorie DOT com
Subject: Re: DJGPP BUG ?
In-Reply-To: <5j5n1j$lph@news.network.com>
Message-ID: <Pine.OSF.3.96.970418062103.11389A-100000@alpha.hut.fi>
MIME-Version: 1.0

On 17 Apr 1997, Mike Collins wrote:
> Am I doing something wrong? Why does the following not work?
> #include <stdio.h>
> #include <io.h>
> 
> 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 <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.


Hope this helps,
Samppa

Samuli Takala    Samuli DOT Takala AT hut DOT fi     finger -l tax AT hut DOT fi for long version

- Raw text -


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