F1e3ab214a976a39cfd713bc93deb10f

faster / better / sexier?

int
File_size(char *path) {
  FILE *fp;
  if (fp = fopen(path, "r")) {
    fseek(fp, 0, SEEK_END);
    int size = ftell(fp);
    fclose(fp);
    return size;
  }
  return -1;
}

Refactorings

No refactoring yet !

B94d3947daf98d3d6c0dd5b50215e4af

Vidar Hokstad

August 5, 2009, August 05, 2009 19:52, permalink

No rating. Login to rate!

Your example is limited to files up to 2GB; unfortunately I don't think you check for the size of larger files portably. If larger files matter to you, and you're on a Unix type platform, look at fseeko, lseek/lseek64, lstat.

F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

August 8, 2009, August 08, 2009 04:10, permalink

No rating. Login to rate!

Nah shouldnt be an issue, good to know though

A8d3f35baafdaea851914b17dae9e1fc

Adam

September 11, 2009, September 11, 2009 15:33, permalink

1 rating. Login to rate!
int File_size(const char *path)
{
    struct stat info;
    
    if (stat(path, &info) == 0) {
        return info.st_size;
    } else {
        return -1;
    }
}

Your refactoring





Format Copy from initial code

or Cancel