Determine the size of a file

From CodeCodex

Revision as of 22:36, 25 March 2012 by Nostromo (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Implementations[edit]

Bash[edit]

$ wc -c filename

C[edit]

#include <stdio.h>
int main()
{  FILE *FileToCheck;
   long filesize;
   FileToCheck = fopen("filename", "rb");
   fseek(FileToCheck, 0, SEEK_END);
   filesize = ftell(FileToCheck);
   fclose(FileToCheck);
   return 0;  }

Common Lisp[edit]

(with-open-file (file "filename") (file-length file))

Erlang[edit]

filelib:file_size(Filename).

Java[edit]

    File file = new File("infilename");
    
    // Get the number of bytes in the file
    long length = file.length();

Perl[edit]

my $file_size = -s 'filename';

PHP[edit]

$file_size = filesize('filename');

Python[edit]

TheFileSize = os.path.getsize(TheFileName)

Ruby[edit]

size = File.size(file_name)

Seed7[edit]

size := fileSize(file_name);

Tcl[edit]

proc filesize f {
  file stat $f a
  return $a(size)
}

Zsh[edit]

zmodload -i zsh/stat
zstat +size filename