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