home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / ANSIFLEN.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  1KB  |  75 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  FLENGTH.C - a simple function using all ANSI-standard functions
  5. **              to determine the size of a file.
  6. **
  7. **  Public domain by Bob Jarvis.
  8. */
  9.  
  10. #include "snipfile.h"
  11.  
  12. #if defined(__cplusplus) && __cplusplus /* C++ version follows */
  13.  
  14.  #include <fstream.h>
  15.  
  16.  long flength(char *fname)
  17.  {
  18.        long length = -1L;
  19.        ifstream ifs;
  20.  
  21.        ifs.open(fname, ios::binary);
  22.        if (ifs)
  23.        {
  24.              ifs.seekg(0L, ios::end) ;
  25.              length = ifs.tellg() ;
  26.        }
  27.        return length;
  28.  }
  29.  
  30. #else /* Straight C version follows */
  31.  
  32.  long flength(char *fname)
  33.  {
  34.        long length = -1L;
  35.        FILE *fptr;
  36.  
  37.        fptr = fopen(fname, "rb");
  38.        if(fptr != NULL)
  39.        {
  40.              fseek(fptr, 0L, SEEK_END);
  41.              length = ftell(fptr);
  42.              fclose(fptr);
  43.        }
  44.  
  45.        return length;
  46.  }
  47.  
  48. #endif /* C++ */
  49.  
  50. #ifdef TEST
  51.  
  52. #ifdef __WATCOMC__
  53.  #pragma off (unreferenced);
  54. #endif
  55. #ifdef __TURBOC__
  56.  #pragma argsused
  57. #endif
  58.  
  59. main(int argc, char *argv[])
  60. {
  61.       char *ptr;
  62.       long len;
  63.  
  64.       while (--argc)
  65.       {
  66.             len = flength(ptr = *(++argv));
  67.             if (-1L == len)
  68.                   printf("\nUnable to get length of %s\n", ptr);
  69.             else  printf("\nLength of %s = %ld\n", ptr, len);
  70.       }
  71.       return 0;
  72. }
  73.  
  74. #endif /* TEST */
  75.