home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / std / c / 2564 < prev    next >
Encoding:
Internet Message Format  |  1992-09-03  |  2.0 KB

  1. Path: sparky!uunet!munnari.oz.au!goanna!ok
  2. From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Operations on Files
  5. Keywords: ANSI C, files
  6. Message-ID: <14335@goanna.cs.rmit.oz.au>
  7. Date: 3 Sep 92 12:10:40 GMT
  8. References: <robertl.4.715452734@county.lmt.mn.org>
  9. Organization: Comp Sci, RMIT, Melbourne, Australia
  10. Lines: 40
  11.  
  12. In article <robertl.4.715452734@county.lmt.mn.org>, robertl@county.lmt.mn.org (ROBERT LAUMEYER) writes:
  13. >     The ANSI C standard seems to have a rather odd mix of file 
  14. > operations.  Why didn't they add (over K&R) some other useful file 
  15. > operations such as fileexist, getfirst, getnext, etc?  It seems that 
  16. > these could not be much more dependent on the underlying file system than 
  17. > rename or remove.
  18.  
  19. What are getfirst and getnext?  If they are the MS-DOS functions, don't
  20. forget that
  21. (a) wild-cards are very OS-dependent.  (Indeed, not all UNIX shells offer
  22.     the same set of wild-cards.)
  23. (b) When multiple threads may modify the file system (e.g. a multi-user
  24.     system, or a file server accessed by many PCs), it is not altogether
  25.     clear what getnext should do when the file system has _changed_ since
  26.     the original call to getfirst.
  27. (c) there are _other_ file system interfaces, such as the ftw() function,
  28.     with at least as good a claim to standardisation.
  29.  
  30. Basically, if we had waited for a really good set of file system operations
  31. we'd _still_ be waiting.
  32.  
  33. As for fileexist, what does that _mean_?  Does a file "exist" for you
  34. if you are not permitted to inspect the directory containing its name?
  35. Even if a file exists, if you are about to run out of file descriptors
  36. or stream buffers you may not be able to open it.
  37.  
  38. A reasonably good workaround is
  39.  
  40.     int file_exists(const char *file_name)
  41.         {
  42.         FILE *stream;
  43.         stream = fopen(file_name, "rb");
  44.         if (stream != NULL) fclose(stream);
  45.         return stream != NULL;
  46.         }
  47.  
  48. This at least answers a useful question:  "would I be able to open this file?"
  49. -- 
  50. You can lie with statistics ... but not to a statistician.
  51.