home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 5001 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: comma.rhein.de!yaps!arno
  2. From: arno@yaps.rhein.de (Arno Eigenwillig)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: File sizes
  5. Message-ID: <zrNcy*T5g@yaps.rhein.de>
  6. Date: Thu, 07 Mar 1996 21:32:19 +0100
  7. References: <4gd9pu$cpr@mn5.swip.net> <WXQ7y*5+f@yaps.rhein.de> <4gjs20$i68@btmpjg.god.bel.alcatel.be>
  8.  <W7s9y*i1g@yaps.rhein.de> <4h66o8$egk@btmpjg.god.bel.alcatel.be>
  9. Organization: Yet Another Private Site in Meckenheim, Germany
  10. X-Copyright: This article may not be distributed on a CD-ROM
  11.  or in printed form without prior written consent of the author.
  12. X-Newsreader: Arn V 1.04
  13.  
  14. In article <4h66o8$egk@btmpjg.god.bel.alcatel.be>,  writes:
  15.  
  16. > ..tell me: what is the correct ANSI-way of getting the correct size
  17. > of a file, even files that are not opened in binary mode?
  18.  
  19. unsigned long c = ~0; /* indicates error */
  20.  
  21. /* If f is a text or binary file, pipe, console etc.: */
  22. c = 0;
  23. while (fgetc(f) != EOF) {
  24.     c++;
  25. }
  26.  
  27. /* If f is a binary real file: */
  28. if (fseek(f, 0, SEEK_END) != -1) {
  29.     c = ftell(f);
  30. }
  31.  
  32. /* May or may not work: */
  33. fseek(f, 0, SEEK_SET); /* -1 on error */
  34.  
  35. The problem is that ANSI-C file I/O does not differenciate between
  36. real files and other stuff and that fseek()/ftell() usually will
  37. operate with OS-level offsets into the file, but what calls to fgetc()
  38. return may well be different from what the file looks like at OS
  39. level: The file I/O functions might have to translate end-of-line
  40. sequences to '\n' and block-oriented files into streams. Therefore,
  41. there is no 1:1 correspondance of ftell() results and a number of
  42. chars read or written.
  43.  
  44. A function like fstat() does just not exist on ANSI level.
  45.  
  46. -- __
  47. __/// Arno Eigenwillig /\ <arno@yaps.rhein.de> \/ PGP key available.
  48. \XX/   V+49-2225-5870  /\ <Arnooo @ #amigager> \/ MIME 8bit welcome.
  49.