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
73
74
75
76
77
78
|
/*
** FSIZE.C - Determine apparent file size of buffered file. Returns size
** corrected for text mode character translation.
**
** public domain demo by Bob Stout
*/
#include <stdio.h>
#include <stdlib.h>
long fsize(FILE *fp)
{
size_t bufsize, bytes_read;
char *bufptr;
long size = 0L, pos;
for (bufsize = 0x8000; NULL == (bufptr = malloc(bufsize)); bufsize /= 2)
;
if (!bufptr)
return -1L;
pos = ftell(fp);
do
{
bytes_read = fread(bufptr, sizeof(char), bufsize, fp);
size += bytes_read;
} while (bytes_read);
free(bufptr);
fseek(fp, pos, SEEK_SET);
return size;
}
#ifdef TEST
#include <string.h>
#ifdef MSDOS
#define fl(x) filelength(x)
#define getsize(fp) fl(fileno(fp))
#else
#define fl(x) puts("Install compiler-specific file length function here")
#define getsize(fp) fl(fp)
#endif
int main(int argc, char *argv[])
{
FILE *fp;
long size, csize, lsize;
char buf[256];
while (--argc)
{
if (NULL == (fp = fopen(*++argv, "r")))
printf("Can't open %s\n", *argv);
size = getsize(fp);
printf("\n\"Real\" size of %s is %ld\n", *argv, size);
for (csize = 0L; EOF != fgetc(fp); ++csize)
;
rewind(fp);
for (lsize = 0L; !feof(fp); )
{
if (NULL != fgets(buf, 256, fp))
lsize += strlen(buf);
}
rewind(fp);
printf("fsize() returned a size = %s is %ld\n",
*argv, fsize(fp));
printf("Reading chars returned an apparent size of %ld\n",
csize);
printf("Reading lines returned an apparent size of %ld\n",
lsize);
}
}
#endif
|