delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/17/21:30:41

From: e-mail DOT address AT end DOT of DOT text (Mike Collins)
Newsgroups: comp.os.msdos.djgpp
Subject: DJGPP BUG ?
Date: 17 Apr 1997 17:35:47 GMT
Organization: Storage Technology Limited
Lines: 61
Message-ID: <5j5n1j$lph@news.network.com>
NNTP-Posting-Host: 129.80.178.182
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Am I doing something wrong? Why does the following not work?
(I already deleted the original, so what I am writing here is 
not
compiler tested, but you'll get the idea)

#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

  printf("%d", (int)(filelength(fileno(fp))));  // prints "0"
                                                // - WHY?
  fclose(fp);
  fp = fopen("junk", "rb+");
  printf("%d", (int)(filelength(fileno(fp))));  
  // prints 26 as expected
  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!
}

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.

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! 

Thanks to anyone who responds,

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