Debian Codegraph @

research/fsize.c

raw symbols SCIP
1 /*
2 The issue here was to avoid fseek() and friends to get away
3 from potential issue with how long "long" really is. So we
4 "abuse" freopen() in append mode to get to the end of the
5 file once we establish it exists at all with fopen() in read
6 mode. Pretty convoluted, but probably the best way. Comments
7 welcome! :-)
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 int file_size(const char *name, fpos_t *size)
14 {
15 int status = -1;
16 FILE *f = fopen(name, "r+");
17 if (f != NULL) {
18 FILE *g = freopen(NULL, "a+", f);
19 if (g != NULL) {
20 if (fgetpos(g, size) == 0) {
21 status = 0;
22 }
23 fclose(g);
24 }
25 }
26 return status;
27 }
28
29 int main(int argc, char *argv[])
30 {
31 printf("Filename %s\n", argv[1]);
32 fpos_t size;
33 if (file_size(argv[1], &size) == 0) {
34 printf("Length of file %s is %ld!\n", argv[1], (long)size);
35 return EXIT_SUCCESS;
36 }
37 else {
38 printf("File %s not found!\n", argv[1]);
39 return EXIT_FAILURE;
40 }
41 }