0

I need to get the dimension of a file, so I wrote this code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {

FILE *file;
char path[256];

long int fsize;

printf("Insert path of file: ");
fgets(path, 256, stdin);
path[strlen (path) - 1] = '\0';

file = fopen( path ,"rb");

if (file == NULL) {
perror("File 1 error: ");
exit(0);
}

fseek(file,0,SEEK_END);
fsize= ftell(file);
rewind(file);

printf("Dimension: %ld Bytes\n",fsize );

return 0;
}

The code works, however when I try to open a big file (13 GB) ftell returns 0. I'm using Windows 10 x64. Is there a limit on the number of bytes that fopen can open or fseek refuses to operate on big files?

  • Perhaps you could use [_stat](https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx) which is Windows specific. POSIX defines a [stat](http://pubs.opengroup.org/onlinepubs/009695399/functions/stat.html) -perhaps usable with `fileno` – Basile Starynkevitch Feb 12 '18 at 19:20
  • `fgets` will always produce a null terminated string, there's no need to add a null. Unless it failed, you're not checking for that. – Schwern Feb 12 '18 at 19:25
  • Formatting/indentation:( – Martin James Feb 12 '18 at 19:26
  • What's `sizeof(long)` on your machine? A `long` is only guaranteed to be at least 32 bits, not big enough to hold 13 billion. And is `fseek` succeeding? – Schwern Feb 12 '18 at 19:27
  • @Schwern: Regarding your first comment, I'm guessing the intent is to trim off the '\n'. I don't believe it succeeds in doing that, however. – Fred Larson Feb 12 '18 at 19:28

0 Answers0